【问题标题】:Custom Android Spinner Layout Issue自定义 Android Spinner 布局问题
【发布时间】: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" />

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

Please click this Link to see the Sample Output

【问题讨论】:

  • 微调器弹出窗口也可以正常加载,但在一秒钟内,布局会刷新以根据项目文本包装宽度。

标签: java android xml spinner


【解决方案1】:

当你使用权重时你应该设置android:layout_width="0dp"

【讨论】:

  • 我也试过了,但输出没有区别。
  • 尝试使用布局参数以编程方式为 textview 和 LinearLayout 设置布局宽度
  • LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); tvOption.setLayoutParams(layoutParams);
  • 这些也没有运气。如上所述,微调器弹出窗口已正确加载,但在加载后,布局受到干扰。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多