how can i change spinner background color?

為{幸葍}努か 提交于 2019-12-24 00:38:55

问题


How can i change spinner background color ?

this black color from style .

How can i change style color for spinner pop up?

I want to change background color white in place of black How can i change ?

spinnner

<Spinner
    android:id="@+id/spinner"
    android:layout_width="100dp"
    android:layout_marginTop="1dp"
    android:layout_height="wrap_content"
    android:background="@drawable/spinner_bg"
    android:layout_marginLeft="1dp"/>

spinnerbg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@color/InputBg1" />
    </item>
    <item android:gravity="center_vertical|right" android:right="8dp">
        <layer-list>
            <item android:width="12dp" android:height="12dp" android:background="#fff" android:gravity="center" android:bottom="10dp">
                <rotate
                    android:fromDegrees="45"
                    android:toDegrees="45">
                    <shape android:shape="rectangle">
                        <solid android:color="#ffffff" />
                        <stroke android:color="#ffffff" android:width="1dp"/>
                    </shape>
                </rotate>
            </item>
            <item android:width="20dp" android:height="10dp" android:bottom="21dp" android:background="#fff" android:gravity="center">
                <shape android:shape="rectangle">
                    <solid android:color="@color/InputBg1"/>
                </shape>
            </item>
        </layer-list>
    </item>
</layer-list>

回答1:


Try this..Change Style in In res/values/styles.xml::-

<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:spinnerDropDownItemStyle">@style/mySpinnerItemStyle</item>
</style>

<style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
    <item name="android:textColor">@android:color/white</item>
</style>




回答2:


You can set the spinners background color in xml like this:

android:background="YOUR_HEX_COLOR_CODE"

and if you use the drop down menu with you spinner you can set its background color like this:

android:popupBackground="YOUR_HEX_COLOR_CODE"

To change text color I recommended to use custom text layout

Give your customized color and size to text in this file.

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:gravity="left"  
    android:textColor="YOUR_HEX_COLOR_CODE"         
    android:padding="5dip"
    />

Now use this file to show your spinner items like:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);

You don't need to set the drop down resource. It will take spinner_item.xml only to show your items in spinner.




回答3:


You can simply add the following code inside <Spinner/> to change popup background.

android:popupBackground="COLOR_CODE"

Now the code should be as follow,

<Spinner
    android:id="@+id/spinner"
    android:layout_width="100dp"
    android:layout_marginTop="1dp"
    android:layout_height="wrap_content"
    android:background="@drawable/spinner_bg"
    android:layout_marginLeft="1dp"
    android:popupBackground="COLOR_CODE"
/>



回答4:


Create style like this in folder res/values/styles.xml

<style name="spinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
    <item name="android:textColor">Put text color code here</item>
    <item name="android:background">Put background color code here</item>
</style>  

Apply to your spinner

<Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/spinnerItemStyle"/>



回答5:


Couple of things need to do with customization spinner as per below :-

spinner_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>

<color android:color="@color/InputBg1" />
</item>
<item android:gravity="center_vertical|right" android:right="8dp">
<layer-list>
<item android:width="12dp" android:height="12dp"  android:gravity="center" android:bottom="10dp">
    <rotate
        android:fromDegrees="45"
        android:toDegrees="45">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
            <stroke android:color="#ffffff" android:width="1dp"/>
        </shape>
    </rotate>
</item>
<item android:width="20dp" android:height="10dp" android:bottom="21dp" android:gravity="center">
    <shape android:shape="rectangle">
        <solid android:color="@color/InputBg1"/>
    </shape>
</item>
</layer-list>
</item>
</layer-list>

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="@style/spinnerItemStyle"
    android:maxLines="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#ffffff"
    android:ellipsize="marquee"
    android:textAlignment="inherit"/>

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="@style/spinnerItemStyle"
    android:maxLines="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="#000000"
    android:ellipsize="marquee"/>

spinner

<Spinner
android:id="@+id/spinner"
android:layout_width="100dp"
android:popupBackground="#ffffff"
android:layout_marginBottom="1dp"
android:layout_height="wrap_content"
android:textColor="#ffff"
style="@style/spinnerItemStyle"
android:background="@drawable/spinner_bg"
android:layout_marginLeft="1dp" />

style.xml

<style name="spinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
    <item name="android:textColor">#000000</item>
</style>

Creating adapter for spinner

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);




回答6:


Try below code

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#FFAA00"/>
        </shape>
    </item>
    <item 
        android:state_selected="true">
        <shape android:shape="rectangle">
            <solid android:color="#FFAA00"/>
        </shape>
    </item>
</selector>

And here is your spinner

<Spinner
   android:id="@+id/sp_data"
   android:background="@drawable/mybg"
   android:layout_width="80dp"
   android:layout_height="wrap_content"
   android:textColor="#0000FF"/>

Happy Coding..!!




回答7:


Add the following in your styles.xml to change the background color and text color of dropdown.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
      <!-- Spinner style -->
    <item name="android:spinnerStyle">@style/AppTheme.spinnerStyle</item>
    <!-- Spinner dropdown style-->
     <item name="android:spinnerDropDownItemStyle">@style/AppTheme.spinnerDropDownItemStyle</item>

</style>
<style name="AppTheme.spinnerStyle" parent="@android:style/Widget.Material.Light.Spinner">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:background">@color/colorPrimary</item>
</style>
<style name="AppTheme.spinnerDropDownItemStyle" parent="@android:style/Widget.Material.DropDownItem.Spinner">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:background">@color/colorPrimary</item>
</style>

In your AndroidManifest.xml add the theme. (Check the value of android:theme)

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">


来源:https://stackoverflow.com/questions/43255262/how-can-i-change-spinner-background-color

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