Cannot see selected text android AppcompactSpinner

橙三吉。 提交于 2019-12-25 06:47:35

问题


I have put an app compact spinner in my application. I am unable to see the selected text(I think this is because the colour of the selected text becomes white, i.e same as the background colour). I am able to see the text in the dropdown.

support-design - 23.3.0

minSdkVersion: 15

targetSdkVersion 23

My Spinner code

<android.support.v7.widget.AppCompatSpinner
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/university_spinner">
</android.support.v7.widget.AppCompatSpinner>

See the images below to understand the error


回答1:


Look Here is step By step solution.

Step : 1 ) define AppCompactSpinner in your xml file

 <android.support.v7.widget.AppCompatSpinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/university_spinner">
    </android.support.v7.widget.AppCompatSpinner>

Step : 2 ) create two different xml file.

       xml file : spinner_item.xml

               <TextView xmlns:android="http://schemas.android.com/apk/res/android"
                          android:id="@android:id/text1"
                          android:layout_width="match_parent"
                  android:layout_height="wrap_content"
              android:textSize="14sp"
              android:background="#FFFFFF"
              android:textColor="#000000" />


       xml file : spinner_dropdown_item.xml

                <?xml version="1.0" encoding="utf-8"?>
                <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
                     android:id="@android:id/text1"
                     style="?android:attr/spinnerDropDownItemStyle"
                     android:singleLine="true"
                     android:layout_width="match_parent"
                     android:layout_height="40dp"
                     android:ellipsize="marquee"
                     android:textColor="#000000"/>

Step : 3 ) java code : declaration

AppCompatSpinner appCompatSpinner;
String[] skills = {"Australian National University","Monash University"};

           inside OnCreate


appCompatSpinner = (AppCompatSpinner)findViewById(R.id.university_spinner);

    ArrayAdapter staticAdapter = new ArrayAdapter(MainActivity.this, R.layout.spinner_item, skills);
    staticAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
    appCompatSpinner.setAdapter(staticAdapter);

Step : 4 ) Without Select Text output :

Step : 5 ) With Select Text output :




回答2:


Hi found out the problem was in the file android.R.layout.simpler_spinner_item as it had not given the text-Color. So I just copied that code and gave a black text-color to it.

spinner_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@android:id/text1"
 style="?android:attr/spinnerItemStyle"
 android:singleLine="true"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:ellipsize="marquee"
 android:textColor="#000000"
 android:textAlignment="inherit"/>


来源:https://stackoverflow.com/questions/37045206/cannot-see-selected-text-android-appcompactspinner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!