在LinearLayout中垂直添加视图

问题描述 投票:1回答:1

我正在尝试在views中垂直添加LinearLayout,如下图所示:

enter image description here

下面的代码给我这样的东西:

enter image description here

 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>
android view android-linearlayout
1个回答
0
投票

您应该只增加资源。请尝试下面的代码。

inflater.inflate(R.layout.suggested_color_item_center, null);
© www.soinside.com 2019 - 2024. All rights reserved.