简体   繁体   English

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

[英]How to make a disabled spinner not to look disabled?

I have made a spinner and when there is only one item on it I want to disable it, but I don't like the grayish look the text gets when it's disabled. 我做了一个微调器,当它只有一个项目时,我想禁用它,但我不喜欢文本在禁用时获得的灰色外观。 I already tried to use .setClickable(false); 我已经尝试过使用.setClickable(false); but it did not work. 但它不起作用。 I have seen a lot of posts with the other way around having a disabled one with look different than the default one, but it didn't help me. 我已经看过很多帖子,其他方面有一个看起来不同于默认值的残疾人,但它没有帮助我。
My code right now is: 我的代码现在是:

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;

but I have no idea on how to get the Enable(false), look like a Enable(true) without being selectable. 但是我不知道如何获得Enable(false),看起来像一个Enable(true)而不是可选择的。

I hope this will helpful to you. 我希望这对你有所帮助。

In xml 在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>

And In java: 在java中:

This is my code, which I tested. 这是我的代码,我测试过。

 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);
    }

Just remove the listener in order not to do anything but keeping the default behaviour. 只需删除侦听器,以便除了保持默认行为之外不做任何事情。

spinner.setOnClickListener(null);

If you want to enable the spinner again, just add once again the listener 如果要再次启用微调器,只需再次添加侦听器即可

spinner.setOnClickListener(myListener);

Override with a custom styled background with no disabled drawable. 使用自定义样式背景覆盖,没有禁用的drawable。

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

res/drawable/spinner_background.xml RES /抽拉/ 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" />

The drawables will depend on the style of your app. drawables将取决于您的应用程序的风格。 You can generate new holo style drawables here for example. 例如,您可以在此处生成新的全息样式绘图。

As for the text colour, this should be very similar, adding a similar xml descriptor:- 至于文本颜色,这应该非常相似,添加一个类似的xml描述符: -

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

res/values/spinner_text.xml RES /值/ 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>

not even sure if that is needed, you may be able to set the color directly: 甚至不确定是否需要,您可以直接设置颜色:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM