android 动态创建View

根据需求,有的时候需要进行动态创建布局,也就是xml中的布局需要通过addView()的形式动态的进行创建。如下图:

这里写图片描述

图中红色部分:如果在xml中,应该是这样的

<LinearLayout
                android:id="@+id/ll_facility_info_create"
                android:layout_width="match_parent"
                android:padding="20dp"
                android:orientation="vertical"
                android:background="@color/calendar_color_white"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:text="设施服务"
                    android:textColor="#000000"
                    android:gravity="center_vertical"
                    android:textSize="@dimen/font20"/>

                <!--动态创建-->
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="30dp"
                        android:text="网络"
                        android:textColor="#000000"
                        android:gravity="center_vertical"/>
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:orientation="horizontal"
                        android:layout_height="30dp">
                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center_vertical"
                            android:text="wifi免费"
                            />
                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center_vertical"
                            android:text="wifi免费"
                            />
                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center_vertical"
                            android:text="wifi免费"
                            />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:orientation="horizontal"
                        android:layout_height="30dp">
                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center_vertical"
                            android:text="wifi免费"
                            />
                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:visibility="invisible"
                            android:gravity="center_vertical"
                            android:text="wifi免费"
                            />
                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:visibility="invisible"
                            android:gravity="center_vertical"
                            android:text="wifi免费"
                            />

                    </LinearLayout>
                </LinearLayout>

            </LinearLayout>

而java代码:

//    动态添加设施服务
    private View addFacilityLayout(){
//        初始化布局参数
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//        创建最外层的LinearLayout
        LinearLayout linearLayout = new LinearLayout(this);
//        设置布局参数
        linearLayout.setLayoutParams(layoutParams);
//        设置子View的Linearlayout
        linearLayout.setOrientation(LinearLayout.VERTICAL);

//        第一个子View  TextView
//        设置子View的布局
        LinearLayout.LayoutParams viewLayoutParams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                DensityUtils.dip2px(context,30));
//        创建子View TextView
        TextView textView = new TextView(this);
//        设置内容的位置
        textView.setGravity(Gravity.CENTER_VERTICAL);
//        设置字体颜色
        textView.setTextColor(Color.BLACK);

        textView.setText("网络");
//        设置布局参数
        textView.setLayoutParams(viewLayoutParams1);
//        将第一子View添加到最外层的linearlayout中
        linearLayout.addView(textView);

        for (int i = 0; i < 2; i++) {
            //        第二个子View  LinearLayout
            LinearLayout.LayoutParams viewLayoutParams2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    DensityUtils.dip2px(context,30));

            LinearLayout linearLayout1 = new LinearLayout(this);

            linearLayout1.setLayoutParams(viewLayoutParams2);
//        设置子View的Linearlayout
            linearLayout1.setOrientation(LinearLayout.HORIZONTAL);

            //        第二个子View  LinearLayout中的TextView
            for (int j = 0; j < 3; j++) {
//          设置子View的布局
                LinearLayout.LayoutParams vp1 = new LinearLayout.LayoutParams(0,DensityUtils.dip2px(context,30),1.0f);
//        创建子View TextView
                TextView tv1 = new TextView(this);
//        设置内容的位置
                tv1.setGravity(Gravity.CENTER_VERTICAL);
//        设置布局参数
                tv1.setLayoutParams(vp1);
                //        设置字体颜色
                tv1.setText("wifi网络");
//                添加子view
                linearLayout1.addView(tv1);
            }
//            添加子View
            linearLayout.addView(linearLayout1);
        }
        return linearLayout;
    }

调用:

 LinearLayout ll_facility_info_create = findViewById(R.id.ll_facility_info_create);
 ll_facility_info_create.addView(addFacilityLayout());
展开阅读全文

没有更多推荐了,返回首页