我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);
}
<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);
}