Java 自定义Android微调器布局问题
我正在定制一个android Spinner小部件。我试图对列表项使用不同的背景色。这是可行的,但当微调器控件中有更多项时,列表项不会水平占据全部空间,因此在弹出视图中显示空白 对于少量项目,它可以正常工作 我尝试了自定义和默认布局自定义 JAVA自定义微调器视图代码Java 自定义Android微调器布局问题,java,android,xml,spinner,Java,Android,Xml,Spinner,我正在定制一个android Spinner小部件。我试图对列表项使用不同的背景色。这是可行的,但当微调器控件中有更多项时,列表项不会水平占据全部空间,因此在弹出视图中显示空白 对于少量项目,它可以正常工作 我尝试了自定义和默认布局自定义 JAVA自定义微调器视图代码 @Override public View getView(int pos, View view, ViewGroup parent) { Layou
@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" />
当有滚动条时,我希望微调器弹出窗口使用背景色的全部可用宽度
当您使用权重时,您应该设置android:layout_width=“0dp”我也尝试过,但输出没有差异。尝试使用layout参数NearLayout.LayoutParams LayoutParams=new LinearLayout.LayoutParams以编程方式设置textview和LinearLayout的布局宽度(LinearLayout.LayoutParams.MATCH_父项,LinearLayout.LayoutParams.MATCH_父项);tVoice.setLayoutParams(LayoutParams);这些也不好。如上所述,微调器弹出窗口加载正确,但加载后布局立即受到干扰。微调器弹出窗口加载良好,但在一秒钟内,布局刷新,以按照项目文本包装宽度。