自定义Android Spinner布局问题

时间:2019-05-13 10:43:22

标签: java android xml spinner

我正在自定义一个android Spinner Widget。我试图对列表项使用不同的背景色。这是可行的,但是当微调器控件中有更多项目时,列表项不会在水平方向上占据整个空间,因此在弹出视图上显示空白。

对于少量物品,它可以正常工作。

我尝试了自定义和默认布局自定义。

JAVA自定义微调器查看代码

            @Override
        public View getView(int pos, View view, ViewGroup parent)
        {
            LayoutInflater objInflater = LayoutInflater.from(context);
            view                       = objInflater.inflate(R.layout.spinner_item_colored, null);


            LinearLayout llOption = (LinearLayout) view.findViewById(R.id.llOption);
            TextView tvOption     = (TextView) view.findViewById(R.id.tvOption);


            if (alDefects.get(pos).sNature.equalsIgnoreCase("1"))
                llOption.setBackgroundColor(getResources().getColor(R.color.majorDefect));

            else if (alDefects.get(pos).sNature.equalsIgnoreCase("2"))
                llOption.setBackgroundColor(getResources().getColor(R.color.criticalDefect));

            else
                llOption.setBackgroundColor(getResources().getColor(R.color.minorDefect));


            tvOption.setTextColor(getResources().getColor(R.color.white));
            tvOption.setText(alDefects.get(pos).sDefect);

            return view;
        }

XML代码:

<LinearLayout
android:id="@+id/llOption"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_margin="0dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
    android:id="@+id/tvOption"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_vertical|left"
    android:singleLine="false"
    android:textColor="#444444"
    android:textSize="13dp" />

我希望微调框弹出窗口在有滚动条时使用背景颜色的全部可用宽度。

Please click this Link to see the Sample Output

1 个答案:

答案 0 :(得分:0)

使用重量时,应设置android:layout_width="0dp"