容器是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)' –