当前位置: 代码迷 >> 综合 >> LinearLayout动态添加View
  详细解决方案

LinearLayout动态添加View

热度:55   发布时间:2023-09-28 20:05:38.0
我LinearLayout动态添加View是实现像下面xml文件样式的布局
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#ff0"
        android:layout_weight="1"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#ff0"
        android:layout_weight="1"/>
</LinearLayout
我具体代码是这样写的
linearLayout = new LinearLayout(this);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            for (int i = 0; i < 2; j++) {
                view = View.inflate(LookLiveActivity.this, R.layout.item_vp_tv, null);
                //第一个参数代表横向填充父窗体题,第二个参数纵向为0dp,第三个参数为权重
                view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,0,1));
                linearLayout.addView(view);
            }

  相关解决方案