【发布时间】:2019-05-13 18:57:08
【问题描述】:
我正在定制一个 android Spinner 小部件。我正在尝试对列表项使用不同的背景颜色。这是可行的,但是当微调器控件中有更多项目时,列表项不会水平占据整个空间,因此会在弹出视图上显示空白。
对于少量项目,它工作正常。
我已经尝试过自定义和默认布局自定义。
JAVA 自定义 Spinner 查看代码
@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" />
当有滚动条时,我希望微调器弹出窗口使用背景颜色的全部可用宽度。
【问题讨论】:
-
微调器弹出窗口也可以正常加载,但在一秒钟内,布局会刷新以根据项目文本包装宽度。