容器是LinearLayout中,如何通过代码添加分隔视图
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
在代码中,我想补充意见:
final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// add alpha view to container view
View alphaView = inflater.inflate(R.layout.item_info_alpha, null, false);
container.addView(alphaView);
// add divider view to container view
container.addView(inflater.inflate(R.layout.item_divider, null, false));
// add beta view to container view
View betaView = inflater.inflate(R.layout.item_info_beta, null, false);
container.addView(betaView);
// add divider view to container view
container.addView(inflater.inflate(R.layout.item_divider, null, false));
item_divider.xml
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="8dp"
android:background="#F2F2F4" />
添加代码时分隔符不可见。
如何在将视图添加到线性布局时使此分隔线可见?
为什么当我们通过xml添加查看,它不会叠加在LinearLayout中?
container.addView(inflater.inflate(R.layout.item_divider,null));将不起作用 – purplebee
@purplebee它会,请参阅文档:'https://developer.android.com/reference/android/view/LayoutInflater.html#inflate(int,android.view.ViewGroup)' –