如何使禁用的微调器看起来不禁用?

How to make a disabled spinner not to look disabled?

我做了一个微调器,当它上面只有一个项目时我想禁用它,但我不喜欢文本在禁用时呈现的灰色外观。我已经尝试使用 .setClickable(false); 但它没有用。我看到很多帖子都用另一种方式让禁用的看起来与默认的不同,但这对我没有帮助。
我现在的代码是:

if (list.size() > 1) {
    list.setlistNumber("Select a list");
    list.add(0, list);
    mListSpinner.setClickable(true);
    return list;
} else {
    mAccountSpinner.setEnabled(false);
    // mListSpinner.set;
    t = 1;

但我不知道如何获得 Enable(false),看起来像 Enable(true),但不可选。

希望对您有所帮助。

在xml

           <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:gravity="center">

            <Spinner
                android:id="@+id/spinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp" />

            <LinearLayout
                android:id="@+id/layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00000000"
                android:clickable="false"
                android:orientation="vertical" />
        </RelativeLayout>

在java中:

这是我的代码,我测试过。

 List<String> list = new ArrayList<>();
    list.add("Please select a item");
    list.add("Item1");
    list.add("Item2");
    list.add("Item3");

    LinearLayout layout = (LinearLayout) findViewById(R.id.layout);

    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, list);

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setAdapter(adapter);

    if (list.size() > 1) {
        layout.setClickable(false);
    } else {
        layout.setClickable(true);
    }

只需删除侦听器,以便除了保持默认行为外什么都不做。

spinner.setOnClickListener(null);

如果你想再次启用微调器,只需再次添加监听器

spinner.setOnClickListener(myListener);

用自定义样式的背景覆盖,不禁用可绘制对象。

   <Spinner
      ...
      android:background="@drawable/spinner_background"
      ...
   />

res/drawable/spinner_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- changed from apptheme_spinner_disabled_holo_light -->
<item android:state_enabled="false"
      android:drawable="@drawable/apptheme_spinner_default_holo_light" />
<item android:state_pressed="true"
      android:drawable="@drawable/apptheme_spinner_pressed_holo_light" />
<item android:state_pressed="false" android:state_focused="true"
      android:drawable="@drawable/apptheme_spinner_focused_holo_light" />
<item android:drawable="@drawable/apptheme_spinner_default_holo_light" />

可绘制对象将取决于您的应用程序的样式。例如,您可以生成新的全息样式可绘制对象 here

至于文字颜色,这个应该很相似,加一个类似的xml描述符:-

   <Spinner
      ...
      android:textColor="@color/spinner_text"
      ...
   />

res/values/spinner_text.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_focused="true" android:color="@color/black"/>
   <item android:state_pressed="true" android:state_enabled="false" android:color="@color/black" />
   <item android:state_enabled="false" android:color="@color/black" />
   <item android:color="@color/black"/>
</selector>

不确定是否需要,您可以直接设置颜色:

       <Spinner
      ...
      android:textColor="#000000"
      ...
   />

你可以试试这个:

spinner.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});