我正在尝试在views
中垂直添加LinearLayout
,如下图所示:
下面的代码给我这样的东西:
private void configure(View view) {
LinearLayout palletContainer = view.findViewById(R.id.palette_container);
fillPalletColors(mPalette, palletContainer);
}
private void fillPalletColors(List<Integer> colors, LinearLayout paletteContainer) {
if (getActivity() != null && isAdded()) {
LayoutInflater inflater = getActivity().getLayoutInflater();
for (int color : colors) {
View palletColor = inflater.inflate(R.layout.suggested_color_item_center, paletteContainer, false);
palletColor.setBackgroundColor(color);
paletteContainer.addView(palletColor);
}
}
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:id="@+id/palette_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" />
</LinearLayout>
您应该只增加资源。请尝试下面的代码。
inflater.inflate(R.layout.suggested_color_item_center, null);