精华内容
下载资源
问答
  • LinearLayout提供了几个方法,用作动态添加View特别好用;可以添加View、删除View、删除指定位置View、删除全部View;看代码:public class MainActivity extends AppCompatActivity { private final String TAG =...

    LinearLayout提供了几个方法,用作动态添加View特别好用;

    可以添加View、删除View、删除指定位置View、删除全部View;

    看代码:

    public class MainActivity extends AppCompatActivity {
    
        private final String TAG = MainActivity.class.getSimpleName();
    
        LinearLayout ll;
        private LayoutInflater mInflater;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
            mInflater = LayoutInflater.from(this);
            ll = findViewById(R.id.ll);
            findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "add", Toast.LENGTH_SHORT).show();
                    ll.addView(View.inflate(MainActivity.this, R.layout.item, null)); //添加一个View
                }
            });
            findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "delect", Toast.LENGTH_SHORT).show();
                    ll.removeViewAt(1); //删除指定位置的View
    //                ll.removeView(view); //删除指定View
                }
            });
            findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "delectAll", Toast.LENGTH_SHORT).show();
                    ll.removeAllViews();  //删除全部View
                }
            });
        }
    }

    Xml布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    
        tools:context=".MainActivity">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/ll"
            >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <Button
                android:id="@+id/btn1"
                android:text="添加"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <Button
                android:id="@+id/btn2"
                android:text="删除"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <Button
                android:id="@+id/btn3"
                android:text="清空"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
        </LinearLayout>
    </ScrollView>

    添加的View:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/tv"
            android:background="@color/colorAccent"
            android:layout_width="match_parent"
            android:layout_height="200dp" />
    
    </LinearLayout>
    

    展开全文
  • Android 根据后台数据动态添加view

    千次阅读 2021-11-16 15:44:27
    首先在activity的布局文件中添加一个LinearLayout装需要动态添加view: activity_main.xml <LinearLayout android:id="@+id/ll_bottom" android:layout_width="match_parent" android:layout_height=...

    首先在activity的布局文件中添加一个LinearLayout装需要动态添加的view:

    activity_main.xml

      <LinearLayout
            android:id="@+id/ll_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="50dp"
            android:layout_gravity="bottom"
            android:orientation="horizontal"
            android:background="#fff"
            app:layout_constraintBottom_toBottomOf="parent"></LinearLayout>

    然后在activity中动态获取:

        private ArrayList<View> bottomView = new ArrayList<>();    //保存动态添加的底部按钮
    
        LinearLayout ll_bottom; //装载view 的父布局
    
        private ArrayList<String> data  = new ArrayList<>();    //假数据
    
    
    
       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ll_bottom = findViewById(R.id.ll_bottom);
            initData();
            getBoomView();
        }
    
    
       private void getBoomView() {
    
            bottomView.clear(); //先清除 再添加
            ll_bottom.removeAllViews();
            for(int i = 0;i<data.size();i++){
                final LinearLayout ll= (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.item_bottom_view,null);   //自定义的布局
                ll.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1));//此处设置权重  三个参数:宽,高,权重
    //            final ImageView ivLogo=(ImageView)ll.findViewById(R.id.iv_bottom_logo);
    //            Glide.with(MainActivity.this).load(data.source).into(ivLogo); //如果有图片资源就加载
                final TextView tvBottom=(TextView)ll.findViewById(R.id.tv_bottom);
                tvBottom.setText(data.get(i));
                bottomView.add(ll);
                ll_bottom.addView(ll);
    
            }
    
            for(int a = 0 ; a<data.size();a++) {
                final int finalI = a;
    
                bottomView.get(a).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this,"点击了 "+ data.get(finalI),Toast.LENGTH_SHORT).show();
                    }
                });
            }
    
        }
    
        private void initData() {
            data.clear();
            data.add("首页");
            data.add("商城");
            data.add("发现");
            data.add("我的");
        }
    item_bottom_view.xml:
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="5dp"
        >
    
        <ImageView
            android:id="@+id/iv_bottom_logo"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@mipmap/ic_launcher"
            />
    
        <TextView
            android:id="@+id/tv_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:layout_marginTop="4dp"
            />
    
    
    
    
    </LinearLayout>
    

    效果图:

    展开全文
  • Android 动态添加View 并设置id

    万次阅读 多人点赞 2018-03-22 16:55:27
    点击动态添加View 先找到要添加的view ,然后添加到LinearLayout中 TextView childView1 = (TextView) LayoutInflater .from (MainActivity .this ) .inflate (R .layout .layout _item_view, ...

    效果图

    主页面布局(main_activity.xml)

    LinearLayout 里面加一个Button,注意这里的LinearLayout要有orientation

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="20dp"
        tools:context="com.yechaoa.addview.MainActivity">
    
        <Button
            android:id="@+id/btn_add_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:text="点击添加view"
            android:textSize="20sp"/>
    
    </LinearLayout>
    

    子View布局(layout_item_view.xml)

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="20dp"
        android:textSize="20sp"/>

    点击动态添加View

    先找到要添加的view ,然后添加到LinearLayout中

    TextView childView1 = (TextView) LayoutInflater.from(MainActivity.this)
    .inflate(R.layout.layout_item_view, mLinearLayout, false);
    mLinearLayout.addView(childView1);

    动态设置id

    这里需要先在values文件夹下创建一个ids.xml资源文件

    这里写图片描述

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <item name="text_view_1" type="id">text_view_1</item>
        <item name="text_view_2" type="id">text_view_2</item>
        <item name="text_view_3" type="id">text_view_3</item>
        <item name="text_view_4" type="id">text_view_4</item>
        <item name="text_view_5" type="id">text_view_5</item>
    
    </resources>

    然后通过setId()方法引用这个ids.xml资源文件中的id就行了

    textView1.setId(R.id.text_view_1);


    MainActivity.java

    package com.yechaoa.addview;
    
    import android.graphics.Color;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.TranslateAnimation;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private LinearLayout mLinearLayout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            initView();
    
        }
    
        private void initView() {
            mLinearLayout = findViewById(R.id.linear_layout);
            Button mBtnAddView = findViewById(R.id.btn_add_view);
            mBtnAddView.setOnClickListener(this);
        }
    
        private int childCount = 0;
    
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_add_view:
    
                    childCount++;
    
                    TextView childView1 = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_item_view, mLinearLayout, false);
                    mLinearLayout.addView(childView1);
    
                    TextView textView1 = childView1.findViewById(R.id.text_view);
    
                    switch (childCount) {
                        case 1:
                            textView1.setId(R.id.text_view_1);
                            textView1.setText(String.valueOf("第 " + childCount + " 个view"));
                            initAnimation(textView1, 1);
                            break;
                        case 2:
                            textView1.setId(R.id.text_view_2);
                            textView1.setText(String.valueOf("第 " + childCount + " 个view"));
                            initAnimation(textView1, 2);
                            break;
                        case 3:
                            textView1.setId(R.id.text_view_3);
                            textView1.setText(String.valueOf("第 " + childCount + " 个view"));
                            textView1.setTextColor(Color.RED);
                            break;
                        case 4:
                            textView1.setId(R.id.text_view_4);
                            textView1.setText(String.valueOf("第 " + childCount + " 个view"));
                            break;
                        case 5:
                            TextView textView = new TextView(MainActivity.this);
                            textView.setId(R.id.text_view_5);
                            textView.setTextSize(20);
                            textView.setGravity(Gravity.CENTER);
                            textView.setPadding(20, 20, 20, 20);
                            textView.setText(String.valueOf("第 " + childCount + " 个view"));
                            textView.setTextColor(Color.BLUE);
                            mLinearLayout.addView(textView);
                            break;
                    }
    
                    break;
            }
        }
    
        private void initAnimation(TextView textView, int position) {
            switch (position) {
                case 1:
                    TranslateAnimation mLeftAnimation1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f,
                            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                            0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
                    mLeftAnimation1.setDuration(500);
                    textView.startAnimation(mLeftAnimation1);
                    textView.animate().alpha(1);
                    break;
                case 2:
                    TranslateAnimation mLeftAnimation2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
                            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                            0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
                    mLeftAnimation2.setDuration(500);
                    textView.startAnimation(mLeftAnimation2);
                    textView.animate().alpha(1);
                    break;
            }
    
        }
    
    }
    

    关于动画:Android Animation动画

    展开全文
  • Android 获取View的位置参数:x,y和相对父容器的偏移量以及中心点坐标...首先,看一下动态添加View的方法: 然后,我们能看到,addView又五个重载方法,是: addView(View child) // 直接添你要添加的View addView(...

    Android 获取View的位置参数:x,y和相对父容器的偏移量以及中心点坐标

    你好!

    Android 利用代码动态添加Viw

    首先,看一下动态添加View的方法:
    在这里插入图片描述
    然后,我们能看到,addView又五个重载方法,是:

    addView(View child)   // 直接添你要添加的View
    addView(View child, int index)   //你要添加的View 与 你所想添加到哪个布局层级
    addView(View child, int width, int height)   // 你要添加的View与它的宽和高(单位,px)
    addView(View view, ViewGroup.LayoutParams params)   // 你要添加的View与它的布局参数
    addView(View child, int index, LayoutParams params)  // 你要添加的View 和 它所要添加的布局层级 以及 布局参数
    

    使用方法:
    一般动态添加View,最常用的就是LinearLayout和RelativeLayout了,然后,下面贴出使用方法:

    		//LinearLayout 和 RelativeLayout都能用
    		 ImageView img1 = new ImageView(Context context);
    		 //可以给ImageView 设置一个图片, img1.setImageResource(R.mipmap.***);
             viewGroup.addView(img1);
    

    下面是设置了LayoutParams的:

    		//LinearLayout
    		//new LayoutParams ,宽 180px 高 180px
     	    ImageView img = new ImageView(this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(180,180);
            //给ImagView设置layoutParams
            img.setLayoutParams(layoutParams);
            //ImageView设置图片资源
            img.setImageResource(R.mipmap.ic_launcher);
            //添加至布局中
            viewGroup.addView(img);
            ImageView img2 = new ImageView(this);
            LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(180,180);
            img2.setLayoutParams(layoutParams2);
            img2.setImageResource(R.mipmap.sing_home_star3);
            viewGroup.addView(img2);
    
    	//RelativeLayout
        ImageView img = new ImageView(this);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(180,180);
        img.setLayoutParams(layoutParams);
        img.setImageResource(R.mipmap.ic_launcher);
        viewGroup.addView(img);
        ImageView img2 = new ImageView(this);
        RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(180,180);
        img2.setLayoutParams(layoutParams2);
        img2.setImageResource(R.mipmap.sing_home_star3);
        viewGroup.addView(img2);
    

    下面 是效果图,分别是LinearLayout的两种垂直或水平,然后就是RelatvieLayout,为了可以比对效果,我添加了两个ImageView:
    在这里插入图片描述
    下面是设置了偏移的动态添加View,(创建了一个宽高都是180px的ImageView,然后 ,让它距离顶部150px,距离左边也是150px):

     	     ImageView img = new ImageView(this);
            //new LayoutParams ,宽 180px 高 180px
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(180, 180);
            //给ImagView设置layoutParams
            img.setLayoutParams(layoutParams);
            //ImageView设置图片资源
            img.setImageResource(R.mipmap.ic_launcher);
            //添加至布局中
            ll.addView(img);
            //创建MarginLayoutParams,参数就填imgView的LayoutParams就行
            ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(img.getLayoutParams());
            /*
             * 给这个MarginLayoutParams添加偏移参数  四个参数(一般只需要顶部和左边就能确定View的位置,后两个参数添0即可):
             *       left:第一个参数,是View距离屏幕左边的距离
             *       top:第二个参数,是Veiw距离屏幕顶部的距离
             *       right:第三个参数,是View距离屏幕右边的距离
             *       bottom: 第三个参数,是View距离屏幕底部的距离
             * */
            margin.setMargins(150, 150, 0, 0);
            //创建一个LayoutParams,把MarginLayoutParams填入其中
            LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(margin);
            //给ImageView设置进去
            img.setLayoutParams(layoutParams3);
    
    
            ImageView img2 = new ImageView(this);
            LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(180, 180);
            img2.setLayoutParams(layoutParams2);
            img2.setImageResource(R.mipmap.sing_home_star3);
            ll.addView(img2);
    
            ViewGroup.MarginLayoutParams margin2 = new ViewGroup.MarginLayoutParams(img2.getLayoutParams());
            margin2.setMargins(150, 150, 0, 0);
            LinearLayout.LayoutParams layoutParams4 = new LinearLayout.LayoutParams(margin2);
            img2.setLayoutParams(layoutParams4);
    

    认真看的读者应该已经发现,添加View的时候父布局是LinearLayout或者RelativeLayout的代码的区别就是它的那个LayoutParms是哪个包下面,LinearLayout就用 LinearLayout.LayoutParams,RelativeLayout就用RelativeLayout.LayoutParams,由于代码多,就不贴这种废代码了,下面是效果图:
    在这里插入图片描述
    如果Ui给的是dp,那么下面有一个dp转px的一个的方法:

    	//直接传一个float的单位为dp的值,然后方法会返回单位为px的数,直接设置即可
        public int dp2px(float dpValue) {
            final float scale = DongTaiViewActivity.this.getResources().getDisplayMetrics().density;
            return (int) (dpValue * scale + 0.5f);
        }
    

    关于代码中的屏幕适配

    我们都知道,咱们Android碎片化比较严重,如果就生硬的按照这样只传px为单位,那么做完之后可以说跑一个手机布局就能一个样,所以,我们动态添加view也需要进行屏幕适配,这里的思路就是 ,你有View宽高的px,距离顶部的px和距离左边的px,(dp的话上面有个方法可以转为px),然后我们看一下设计图的尺寸标准,然后用标准尺寸除以屏幕分辨率,然后获取的那个倍数再乘以它的标准尺寸,得到的px就很适配了

    		//获取屏幕分辨率
     	    DisplayMetrics dm = context.getResources().getDisplayMetrics();
            int screenWidth = dm.widthPixels;
            int screenHeight = dm.heightPixels;
    

    然后接下来就按照基准分辨率来进行计算与此设备屏幕分辨率的倍数:

    	 //基准屏幕宽
        private final int standardWith = 750;
        //基准屏幕高
        private final int standradHeight = 1334;
        //屏幕分辨率
        private int screenWidth;
        private int screenHeight;
        //倍数  -- 宽和高与设备分辨率之间的倍数
        private final float multipleWith;
        private final float multipleHeight;
        
        public void calculationB(){
    		    screenWidth = dm.widthPixels;
            	screenHeight = dm.heightPixels;
           	    multipleWith = (float) standardWith / (float) screenWidth;
          	    multipleHeight = (float) standradHeight / (float) screenHeight;
    	}
    

    然后我们有了设计图基准分辨率与设备之间差的倍数,然后也有它的长宽,和偏移量,那么,接下来我们就该算他们适配后的px数(传进来一个宽和高,然后经过计算,将其返回,思路就是,判断标准宽是否大于屏幕宽,如果大于,直接用传的值除以他们之间的倍数,如果小于,就用2减去他们的倍数,然后用传的宽乘这个倍数,高也一样):

        public int[] getAdaptWH(int width, int height) {
            //适配完的宽度
            float sWidth = 0;
            //适配完的高度
            float sHeight = 0;
            if (standardWith > screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width / multipleWith;
                sHeight = (float) height / multipleHeight;
            } else if (standardWith < screenWidth && standradHeight < screenHeight) {
                sWidth = (float) width * (2.0f - multipleWith);
                sHeight = (float) height * (2.0f - multipleHeight);
            } else if (standardWith < screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width * (2.0f - multipleWith);
                sHeight = (float) height / multipleHeight;
            } else if (standardWith > screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width / multipleWith;
                sHeight = (float) height * (2.0f - multipleHeight);
            }
            //standradWH[0] 是宽  standradWH[1] 是高
            int[] standradWH = {(int) sWidth, (int) sHeight};
            return standradWH;
        }
    

    最后,奉上工具类

    对这个屏幕适配我做了一个工具类,名字是PxAdaptionUtils,其中宽度分别可以传单个宽高和集合宽高,以及数组宽高:

    import android.content.Context;
    import android.util.DisplayMetrics;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class PxAdaptionUtils {
        //基准屏幕宽
        private final int standardWith = 750;
        //基准屏幕高
        private final int standradHeight = 1334;
        //屏幕分辨率
        private int screenWidth;
        private int screenHeight;
        private Context context;
        private final float multipleWith;
        private final float multipleHeight;
    
        public PxAdaptionUtils(Context context) {
            this.context = context;
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            screenWidth = dm.widthPixels;
            screenHeight = dm.heightPixels;
            multipleWith = (float) standardWith / (float) screenWidth;
            multipleHeight = (float) standradHeight / (float) screenHeight;
        }
    
        public int[] getAdaptWH(int width, int height) {
            //适配完的宽度
            float sWidth = 0;
            //适配完的高度
            float sHeight = 0;
            if (standardWith > screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width / multipleWith;
                sHeight = (float) height / multipleHeight;
            } else if (standardWith < screenWidth && standradHeight < screenHeight) {
                sWidth = (float) width * (2.0f - multipleWith);
                sHeight = (float) height * (2.0f - multipleHeight);
            } else if (standardWith < screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width * (2.0f - multipleWith);
                sHeight = (float) height / multipleHeight;
            } else if (standardWith > screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width / multipleWith;
                sHeight = (float) height * (2.0f - multipleHeight);
            }
            //standradWH[0] 是宽  standradWH[1] 是高
            int[] standradWH = {(int) sWidth, (int) sHeight};
            return standradWH;
        }
    
        public List<Float> getAdaptWH(List<Float> listWh) {
            //集合从第一个元素开始,每两个元素是一对,索引为偶数的是宽
            List<Float> adaptWHList = new ArrayList<>();
            //看你传来的集合是否成对,如果成对,最后一个值,舍弃
            boolean isPair = true;
            if (listWh.size() % 2 == 0) {
                isPair = true;
            } else {
                isPair = false;
            }
    
            for (int i = 0; i < listWh.size(); i++) {
                float element = listWh.get(i);
                if (isPair) {
                    if (i % 2 == 0) {
                        float adaptWidth = getAdaptWidth(element);
                        adaptWHList.add(adaptWidth);
                    } else {
                        float adaptHeight = getAdaptHeight(element);
                        adaptWHList.add(adaptHeight);
                    }
                } else {
                    if (i == (listWh.size() - 1)) {
                        break;
                    }
                    if (i % 2 == 0) {
                        float adaptWidth = getAdaptWidth(element);
                        adaptWHList.add(adaptWidth);
                    } else {
                        float adaptHeight = getAdaptHeight(element);
                        adaptWHList.add(adaptHeight);
                    }
                }
            }
            return adaptWHList;
        }
    
        public List<Float> getAdaptWH(int[] arrWh) {
            //集合从第一个元素开始,每两个元素是一对,索引为偶数的是宽
            List<Float> adaptWHList = new ArrayList<>();
            //看你传来的集合是否成对,如果成对,最后一个值,舍弃
            boolean isPair = true;
            if (arrWh.length % 2 == 0) {
                isPair = true;
            } else {
                isPair = false;
            }
    
            for (int i = 0; i < arrWh.length; i++) {
                int element = arrWh[i];
                if (isPair) {
                    if (i % 2 == 0) {
                        float adaptWidth = getAdaptWidth(element);
                        adaptWHList.add(adaptWidth);
                    } else {
                        float adaptHeight = getAdaptHeight(element);
                        adaptWHList.add(adaptHeight);
                    }
                } else {
                    if (i == (arrWh.length - 1)) {
                        break;
                    }
                    if (i % 2 == 0) {
                        float adaptWidth = getAdaptWidth(element);
                        adaptWHList.add(adaptWidth);
                    } else {
                        float adaptHeight = getAdaptHeight(element);
                        adaptWHList.add(adaptHeight);
                    }
                }
            }
            return adaptWHList;
        }
    
        private float getAdaptHeight(int height) {
            //适配玩的高度
            float sHeight = 0;
            if (standardWith > screenWidth && standradHeight > screenHeight) {
                sHeight = (float) height / multipleHeight;
            } else if (standardWith < screenWidth && standradHeight < screenHeight) {
                sHeight = (float) height * (2.0f - multipleHeight);
            } else if (standardWith < screenWidth && standradHeight > screenHeight) {
                sHeight = (float) height / multipleHeight;
            } else if (standardWith > screenWidth && standradHeight > screenHeight) {
                sHeight = (float) height * (2.0f - multipleHeight);
            }
            return sHeight;
        }
    
        private float getAdaptWidth(int width) {
            //适配完的宽度
            float sWidth = 0;
            if (standardWith > screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width / multipleWith;
            } else if (standardWith < screenWidth && standradHeight < screenHeight) {
                sWidth = (float) width * (2.0f - multipleWith);
            } else if (standardWith < screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width * (2.0f - multipleWith);
            } else if (standardWith > screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width / multipleWith;
            }
            return sWidth;
        }
    
        private float getAdaptHeight(float height) {
            //适配玩的高度
            float sHeight = 0;
            if (standardWith > screenWidth && standradHeight > screenHeight) {
                sHeight = height / multipleHeight;
            } else if (standardWith < screenWidth && standradHeight < screenHeight) {
                sHeight = height * (2.0f - multipleHeight);
            } else if (standardWith < screenWidth && standradHeight > screenHeight) {
                sHeight = height / multipleHeight;
            } else if (standardWith > screenWidth && standradHeight > screenHeight) {
                sHeight = height * (2.0f - multipleHeight);
            }
            return sHeight;
        }
    
        private float getAdaptWidth(float width) {
            //适配完的宽度
            float sWidth = 0;
            if (standardWith > screenWidth && standradHeight > screenHeight) {
                sWidth = width / multipleWith;
            } else if (standardWith < screenWidth && standradHeight < screenHeight) {
                sWidth = width * (2.0f - multipleWith);
            } else if (standardWith < screenWidth && standradHeight > screenHeight) {
                sWidth = width * (2.0f - multipleWith);
            } else if (standardWith > screenWidth && standradHeight > screenHeight) {
                sWidth = width / multipleWith;
            }
            return sWidth;
        }
    }
    
    

    动态添加View的工具类,里面封装了一些常用的基础控件,如果需要用其他的,也可以自己添加:

    /*
     * 参数  ! ViewTag 是你需要添加什么View
     * 参数 ! viewWith 与 viewHeight 是View的宽和高
     *
     *(addView方法上面有参数详解)
     *
     * */
    public class DynamicAddViewPlus {
        //imageView
        public static final int TagImag = 0x001;
        //TextView
        public static final int TagTextView = 0x002;
        //Button
        public static final int TagButton = 0x003;
        //EditText
        public static final  int TagEditText = 0x004;
        //ImageButton
        public static final int TagImageButton = 0x005;
        //ToggleButton (状态开关按钮 )
        public static final int TagToggleButton = 0x006;
        //CheckBox
        public static final int TagCheckBox = 0x007;
        //TextClock
        public static  final int TagTextClock = 0x008;
    
        // 父布局是RelativeLayout
        public static final int TagFatherRelativeLayout = 0x001;
        // 父布局是LinearLayout
        public static final int TagFratherLinearLayout = 0x002;
    
        //View的宽高的自适应
        public static final int CWRAP_CONTENT = -2;
        //View的宽高铺满全屏
        public static final int CMATCH_PARENT = -1;
    
        // 把View添加至某个图层,默认是0
        private int layoutHierarchy = -1;
    
        private Context context;
        private PxAdaptionUtils pxAdaptationUtils;
        //是否指定添加View的图层
        private boolean isAppoint = false;
    
        public DynamicAddViewPlus(Context context) {
            this.context = context;
            pxAdaptationUtils = new PxAdaptionUtils(context);
        }
    
        /*
         * ViewTag :添加什么View
         * viewWith : view的宽       px
         * viewHeght : view的高      px
         * distanceTop : view与屏幕顶部的距离 px
         * distanceLeft : view与屏幕左边的距离 px
         * pVg : 父ViewGroup , 就是你要往哪个Layout里面添加,传他的id
         * activity : 你在哪个Activity里面添加,添this就行
         * fatherViewGroupTag : 你需要添加的Layout 是 LinearLayout ,还是 RelativeLayout ,直接填 DynamicAddViewPlus.TagFatherRelativeLayout | DynamicAddViewPlus.TagFratherLinearLayout
         *
         * */
        public View addView(int ViewTag, int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag, int layoutHierarchy) {
            isAppoint = true;
    //        View view = null;
            this.layoutHierarchy = layoutHierarchy;
            switch (ViewTag) {
                case TagImag:
                    ImageView img = dtAddImage(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    this.layoutHierarchy = -1;
                    return img;
                case TagTextView:
                    TextView tv = (TextView) dtAddTextView(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    this.layoutHierarchy = -1;
                    return tv;
                case TagButton:
                    Button button = dtAddButton(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    this.layoutHierarchy = -1;
                    return button;
                case TagEditText:
                    EditText editText = dtAddEditText(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    this.layoutHierarchy = -1;
                    return editText;
                case TagImageButton:
                    ImageButton imageButton = dtAddImageButton(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    return imageButton;
                case TagToggleButton:
                    ToggleButton toggleButton = dtAddToggleButton(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    return toggleButton;
                case TagCheckBox:
                    CheckBox checkBox = dtAddCheckBox(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    return checkBox;
             /*   case TagTextClock:
                    TextClock textClock = dtAddTextClock(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    return textClock;*/
                default:
                    return null;
            }
        }
    
        public View addView(int ViewTag, int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
            isAppoint = false;
            switch (ViewTag) {
                case TagImag:
                    ImageView img = dtAddImage(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    isAppoint = false;
                    return img;
                case TagTextView:
                    TextView tv = (TextView) dtAddTextView(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    isAppoint = false;
                    return tv;
                case TagButton:
                    Button button = dtAddButton(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    return button;
                case TagEditText:
                    EditText editText = dtAddEditText(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    return editText;
                case TagImageButton:
                    ImageButton imageButton = dtAddImageButton(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    return imageButton;
                case TagToggleButton:
                    ToggleButton toggleButton = dtAddToggleButton(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    return toggleButton;
                case TagCheckBox:
                    CheckBox checkBox = dtAddCheckBox(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    return checkBox;
             /*   case TagTextClock:
                    TextClock textClock = dtAddTextClock(viewWith, viewHeght, distanceTop, distanceLeft, pVg, activity, fatherViewGroupTag);
                    return textClock;*/
                default:
                    return null;
            }
        }
    
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
        private TextClock dtAddTextClock(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
            TextClock tClock = new TextClock(context);
            if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tClock.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tClock.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tClock.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    tClock.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    tClock.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    tClock.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tClock.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tClock.setLayoutParams(layoutParams);
                }
            } else {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    tClock.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    tClock.setLayoutParams(layoutParams);
                }
            }
            if (isAppoint) {
                pVg.addView(tClock, layoutHierarchy);
            } else {
                pVg.addView(tClock);
            }
            ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(tClock.getLayoutParams());
            int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
            margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
            DisplayMetrics metric = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
                tClock.setLayoutParams(layoutParams2);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
                tClock.setLayoutParams(layoutParams2);
            }
            return tClock;
        }
    
        private CheckBox dtAddCheckBox(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
            CheckBox cb = new CheckBox(context);
            if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    cb.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    cb.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    cb.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    cb.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    cb.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    cb.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    cb.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    cb.setLayoutParams(layoutParams);
                }
            } else {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    cb.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    cb.setLayoutParams(layoutParams);
                }
            }
            if (isAppoint) {
                pVg.addView(cb, layoutHierarchy);
            } else {
                pVg.addView(cb);
            }
            ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(cb.getLayoutParams());
            int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
            margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
            DisplayMetrics metric = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
                cb.setLayoutParams(layoutParams2);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
                cb.setLayoutParams(layoutParams2);
            }
            return cb;
        }
    
        private ImageButton dtAddImageButton(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
            ImageButton imgBtn = new ImageButton(context);
            if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    imgBtn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    imgBtn.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    imgBtn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    imgBtn.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    imgBtn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    imgBtn.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    imgBtn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    imgBtn.setLayoutParams(layoutParams);
                }
            } else {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    imgBtn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    imgBtn.setLayoutParams(layoutParams);
                }
            }
            if (isAppoint) {
                pVg.addView(imgBtn, layoutHierarchy);
            } else {
                pVg.addView(imgBtn);
            }
            ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(imgBtn.getLayoutParams());
            int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
            margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
            DisplayMetrics metric = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
                imgBtn.setLayoutParams(layoutParams2);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
                imgBtn.setLayoutParams(layoutParams2);
            }
            return imgBtn;
        }
    
        private ToggleButton dtAddToggleButton(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
            ToggleButton tBtn = new ToggleButton(context);
            if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tBtn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tBtn.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tBtn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    tBtn.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    tBtn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    tBtn.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tBtn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tBtn.setLayoutParams(layoutParams);
                }
            } else {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    tBtn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    tBtn.setLayoutParams(layoutParams);
                }
            }
            if (isAppoint) {
                pVg.addView(tBtn, layoutHierarchy);
            } else {
                pVg.addView(tBtn);
            }
            ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(tBtn.getLayoutParams());
            int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
            margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
            DisplayMetrics metric = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
                tBtn.setLayoutParams(layoutParams2);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
                tBtn.setLayoutParams(layoutParams2);
            }
            return tBtn;
        }
    
        private EditText dtAddEditText(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
            EditText editT = new EditText(context);
            if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    editT.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    editT.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    editT.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    editT.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    editT.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    editT.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    editT.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    editT.setLayoutParams(layoutParams);
                }
            } else {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    editT.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    editT.setLayoutParams(layoutParams);
                }
            }
            if (isAppoint) {
                pVg.addView(editT, layoutHierarchy);
            } else {
                pVg.addView(editT);
            }
            ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(editT.getLayoutParams());
            int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
            margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
            DisplayMetrics metric = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
                editT.setLayoutParams(layoutParams2);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
                editT.setLayoutParams(layoutParams2);
            }
            return editT;
        }
    
        private Button dtAddButton(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
            Button btn = new Button(context);
            if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    btn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    btn.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    btn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    btn.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    btn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    btn.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    btn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    btn.setLayoutParams(layoutParams);
                }
            } else {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    btn.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    btn.setLayoutParams(layoutParams);
                }
            }
            if (isAppoint) {
                pVg.addView(btn, layoutHierarchy);
            } else {
                pVg.addView(btn);
            }
            ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(btn.getLayoutParams());
            int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
            margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
            DisplayMetrics metric = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
                btn.setLayoutParams(layoutParams2);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
                btn.setLayoutParams(layoutParams2);
            }
            return btn;
        }
    
        private View dtAddTextView(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
            TextView tv = new TextView(context);
            if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tv.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tv.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tv.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    tv.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    tv.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    tv.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tv.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tv.setLayoutParams(layoutParams);
                }
            } else {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    tv.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    tv.setLayoutParams(layoutParams);
                }
            }
            if (isAppoint) {
                pVg.addView(tv, layoutHierarchy);
            } else {
                pVg.addView(tv);
            }
            ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(tv.getLayoutParams());
            int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
            margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
            DisplayMetrics metric = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
                tv.setLayoutParams(layoutParams2);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
                tv.setLayoutParams(layoutParams2);
            }
            return tv;
        }
    
        private ImageView dtAddImage(int viewWith, int viewHeght, int distanceTop, int distanceLeft, ViewGroup pVg, Activity activity, int fatherViewGroupTag) {
            ImageView img = new ImageView(context);
    
            if (viewWith == CWRAP_CONTENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    img.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    img.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CWRAP_CONTENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    img.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    img.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CMATCH_PARENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    img.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    img.setLayoutParams(layoutParams);
                }
            } else if (viewWith == CMATCH_PARENT && viewHeght == CWRAP_CONTENT) {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    img.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    img.setLayoutParams(layoutParams);
                }
            } else {
                if (TagFatherRelativeLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    img.setLayoutParams(layoutParams);
                } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                    int[] adaptWH = pxAdaptationUtils.getAdaptWH(viewWith, viewHeght);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(adaptWH[0], adaptWH[1]);
                    img.setLayoutParams(layoutParams);
                }
            }
            if (isAppoint) {
                pVg.addView(img, layoutHierarchy);
            } else {
                pVg.addView(img);
            }
            ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(img.getLayoutParams());
            int[] adaptWH1 = pxAdaptationUtils.getAdaptWH(distanceLeft, distanceTop);
            margin.setMargins(adaptWH1[0], adaptWH1[1], 0, 0);
            DisplayMetrics metric = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
            if (TagFatherRelativeLayout == fatherViewGroupTag) {
                RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin);
                img.setLayoutParams(layoutParams2);
            } else if (TagFratherLinearLayout == fatherViewGroupTag) {
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(margin);
                img.setLayoutParams(layoutParams2);
            }
            return img;
        }
    }
    
    class PxAdaptionUtils {
        //基准屏幕宽
        private final int standardWith = 750;
        //基准屏幕高
        private final int standradHeight = 1334;
        //屏幕分辨率
        private int screenWidth;
        private int screenHeight;
        private Context context;
        private final float multipleWith;
        private final float multipleHeight;
    
        public PxAdaptionUtils(Context context) {
            this.context = context;
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            screenWidth = dm.widthPixels;
            screenHeight = dm.heightPixels;
            multipleWith = (float) standardWith / (float) screenWidth;
            multipleHeight = (float) standradHeight / (float) screenHeight;
        }
    
        public int[] getAdaptWH(int width, int height) {
            //适配完的宽度
            float sWidth = 0;
            //适配玩的高度
            float sHeight = 0;
            if (standardWith > screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width / multipleWith;
                sHeight = (float) height / multipleHeight;
            } else if (standardWith < screenWidth && standradHeight < screenHeight) {
                sWidth = (float) width * (2.0f - multipleWith);
                sHeight = (float) height * (2.0f - multipleHeight);
            } else if (standardWith < screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width * (2.0f - multipleWith);
                sHeight = (float) height / multipleHeight;
            } else if (standardWith > screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width / multipleWith;
                sHeight = (float) height * (2.0f - multipleHeight);
            }
            //standradWH[0] 是宽  standradWH[1] 是高
            int[] standradWH = {(int) sWidth, (int) sHeight};
            return standradWH;
        }
    
        public List<Float> getAdaptWH(List<Float> listWh) {
            //集合从第一个元素开始,每两个元素是一对,索引为偶数的是宽
            List<Float> adaptWHList = new ArrayList<>();
            //看你传来的集合是否成对,如果成对,最后一个值,舍弃
            boolean isPair = true;
            if (listWh.size() % 2 == 0) {
                isPair = true;
            } else {
                isPair = false;
            }
    
            for (int i = 0; i < listWh.size(); i++) {
                float element = listWh.get(i);
                if (isPair) {
                    if (i % 2 == 0) {
                        float adaptWidth = getAdaptWidth(element);
                        adaptWHList.add(adaptWidth);
                    } else {
                        float adaptHeight = getAdaptHeight(element);
                        adaptWHList.add(adaptHeight);
                    }
                } else {
                    if (i == (listWh.size() - 1)) {
                        break;
                    }
                    if (i % 2 == 0) {
                        float adaptWidth = getAdaptWidth(element);
                        adaptWHList.add(adaptWidth);
                    } else {
                        float adaptHeight = getAdaptHeight(element);
                        adaptWHList.add(adaptHeight);
                    }
                }
            }
            return adaptWHList;
        }
    
        public List<Float> getAdaptWH(int[] arrWh) {
            //集合从第一个元素开始,每两个元素是一对,索引为偶数的是宽
            List<Float> adaptWHList = new ArrayList<>();
            //看你传来的集合是否成对,如果成对,最后一个值,舍弃
            boolean isPair = true;
            if (arrWh.length % 2 == 0) {
                isPair = true;
            } else {
                isPair = false;
            }
    
            for (int i = 0; i < arrWh.length; i++) {
                int element = arrWh[i];
                if (isPair) {
                    if (i % 2 == 0) {
                        float adaptWidth = getAdaptWidth(element);
                        adaptWHList.add(adaptWidth);
                    } else {
                        float adaptHeight = getAdaptHeight(element);
                        adaptWHList.add(adaptHeight);
                    }
                } else {
                    if (i == (arrWh.length - 1)) {
                        break;
                    }
                    if (i % 2 == 0) {
                        float adaptWidth = getAdaptWidth(element);
                        adaptWHList.add(adaptWidth);
                    } else {
                        float adaptHeight = getAdaptHeight(element);
                        adaptWHList.add(adaptHeight);
                    }
                }
            }
            return adaptWHList;
        }
    
        private float getAdaptHeight(int height) {
            //适配玩的高度
            float sHeight = 0;
            if (standardWith > screenWidth && standradHeight > screenHeight) {
                sHeight = (float) height / multipleHeight;
            } else if (standardWith < screenWidth && standradHeight < screenHeight) {
                sHeight = (float) height * (2.0f - multipleHeight);
            } else if (standardWith < screenWidth && standradHeight > screenHeight) {
                sHeight = (float) height / multipleHeight;
            } else if (standardWith > screenWidth && standradHeight > screenHeight) {
                sHeight = (float) height * (2.0f - multipleHeight);
            }
            return sHeight;
        }
    
        private float getAdaptWidth(int width) {
            //适配完的宽度
            float sWidth = 0;
            if (standardWith > screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width / multipleWith;
            } else if (standardWith < screenWidth && standradHeight < screenHeight) {
                sWidth = (float) width * (2.0f - multipleWith);
            } else if (standardWith < screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width * (2.0f - multipleWith);
            } else if (standardWith > screenWidth && standradHeight > screenHeight) {
                sWidth = (float) width / multipleWith;
            }
            return sWidth;
        }
    
        private float getAdaptHeight(float height) {
            //适配玩的高度
            float sHeight = 0;
            if (standardWith > screenWidth && standradHeight > screenHeight) {
                sHeight = height / multipleHeight;
            } else if (standardWith < screenWidth && standradHeight < screenHeight) {
                sHeight = height * (2.0f - multipleHeight);
            } else if (standardWith < screenWidth && standradHeight > screenHeight) {
                sHeight = height / multipleHeight;
            } else if (standardWith > screenWidth && standradHeight > screenHeight) {
                sHeight = height * (2.0f - multipleHeight);
            }
            return sHeight;
        }
    
        private float getAdaptWidth(float width) {
            //适配完的宽度
            float sWidth = 0;
            if (standardWith > screenWidth && standradHeight > screenHeight) {
                sWidth = width / multipleWith;
            } else if (standardWith < screenWidth && standradHeight < screenHeight) {
                sWidth = width * (2.0f - multipleWith);
            } else if (standardWith < screenWidth && standradHeight > screenHeight) {
                sWidth = width * (2.0f - multipleWith);
            } else if (standardWith > screenWidth && standradHeight > screenHeight) {
                sWidth = width / multipleWith;
            }
            return sWidth;
        }
    
    }
    

    感谢观看

    我是入梦,谢谢你的观看我的博客,如果有什么错误,请随时联系我,QQ:897589417

    展开全文
  • Android两种方式实现动态添加View

    千次阅读 2020-05-03 00:17:21
    最近有个需求是在布局上动态添加view,本来想写死的,但是发现这样写后期需求变动后改动很大,不灵活,还得重新写,于是研究了一下在布局上动态添加view的方式。 1.初始化View: 2.初始化事件监听 3.向容器中...
  • android 在LinearLayout中动态添加View

    千次阅读 2019-07-22 09:06:28
    android 在LinearLayout中动态添加View前言了解使用参考完事 前言 在项目中有一个列表,列表里的item里面还有一个未知个数的横向列表 用列表来做,RecyclerView、ListView、GridView都需要另外再写一个Adapter,...
  • ConstraintLayout动态添加View,改变约束

    千次阅读 2019-12-17 09:19:36
    https://www.jianshu.com/p/16e34f919e1a
  • 在ScrollView中动态添加View

    千次阅读 2017-07-21 13:40:26
    需求:要求在ScrollView中动态添加View,并且添加的View可以进行缩进。 思路:一开始的思路是使用ListView动态添加Item的方法来实现,即当需要新增一个View时,就给适配器新增一条数据,并提醒适配器更新数据,如果...
  • 微信小程序 动态添加view组件

    万次阅读 热门讨论 2018-10-17 10:20:51
    在web中,我们动态添加DOM,可以用jQuery的方法,很简单。在微信小程序中怎么实现下面这么需求。 其中,里程数代表上一行到这一行地方的距离(这个不重要);要实现的就是点击增加途径地,就多一行,删除途径地,就...
  • 微信小程序动态添加view

    千次阅读 2019-05-04 13:54:00
    微信小程序动态添加view //index.html <button bindtap='clickMe'> 添加</button> <input type='input' value='{{value}}' placeholder='请输入' bindblur='getValue'></input> &...
  • 使用RelativeLayout动态添加View总结

    千次阅读 2018-01-16 11:26:57
    在很多时候xml里面的布局并不能满足我们的需求。这时候就需要用代码进行动态布局,前些天在对RelativeLayout 进行动态布局时遇到了些问题...1.添加第一个View。 RelativeLayout layout = (RelativeLayout)findViewBy
  • android代码动态添加View

    千次阅读 2018-11-23 13:38:20
    @Override protected void onCreate(Bundle savedInstanceState) { ... //Java代码中动态的生成View //首先我们在onCreate方法中创建一个Button实例: Button button = new Button(this);...
  • LinearLayout动态添加View

    千次阅读 2016-07-06 13:31:08
    我LinearLayout动态添加View是实现像下面xml文件样式的布局  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical">    android:layout_width=...
  • 自定义View-1-动态添加view

    千次阅读 2016-04-23 11:19:19
    //在代码中实现添加布局 布局文件 LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:id="@+i
  • Android 动态添加view或item并获取数据

    千次阅读 2018-09-12 21:27:59
    按照国际惯例,先上图: 核心代码在于以下两点: /** * 添加item */ ... View viewItem = LayoutInflater.from(this).inflate(R.layout.item_add_vip_num, llVipNumContainer,false); ...
  • 添加视图文件的时候有两种方式:1、通过在xml文件定义layout;2、java代码编写 二、前言说明 1.构造xml文件 2.LayoutInflater 提到addview,首先要了解一下LayoutInflater类。这个类最主要的功能就是实现将xml...
  • protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_xinxi); list=new ArrayList&lt;Friendsmoban&gt;();...
  •  public void onClick(View v) {  // 判断当前点击了哪一个  for (int i = 0; i (); i++) {  TextView tv = (TextView) ll_titles.getChildAt(i);  if (v == tv) {  // 让对应的这个textView变红  tv....
  • <LinearLayout android:id="@+id/lay_chart" android:layout_width="500dip" android:layout_height="500dip" android:orientation="vertical"> </LinearLayout>...父view ...
  • 最近在项目中碰到一个需求,在编辑一个红包详情页面的时候,需要动态添加使用条件,并且可以删除,最后提交数据的时候需要获取到条件里面的数据。网上搜了一些答案没有比较满意的解决方法,所以写下此文。 效果图...
  • ![图片说明](https://img-ask.csdn.net/upload/201505/26/1432607386_487062.png)
  • Android LinearLayout动态添加View时如何设置View间的间距  View view = new View(this);  view.setBackgroundResource(R.drawable.selector_dot);  LinearLayout.Lay
  • Android 动态添加View并设置padding

    千次阅读 2017-01-19 15:59:57
    Android 动态添加View并设置padding 动态设置padding LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(10, 10); layoutParams.setMargins(10,0,10,0); iv_point.setLayoutPa
  • * 动态在viewpager中添加view,并实现无限循环 */ public class MainActivity extends AppCompatActivity { private ViewPager list_pager; private List<View> list_view; private viewpageAdapter adpter;...
  • 代码简单,方便学习,加载本地图片和网络图片。
  • 先看看效果有时候,你不知道需求里面有几个View,要根据后台传的个数来动态添加撸代码因为是可以横向滑动的,所有我用一个HorizontalScrollView包裹一个LinearLayout,在LinearLayout里面动态add需要的View布局文件 ...
  • 最近在做动态添加 View 的效果,给动态添加View 设置了 margin 属性,但却总是被忽略,不起作用。原因是出在 LayoutInflater 上。 LayoutInflater 有三种加载方式,但是想要使动态加载的view的属性生效,实际上取...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,070,616
精华内容 428,246
关键字:

动态添加view