Android:需要更改微调器背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20032594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:11:47  来源:igfitidea点击:

Android: Need to change the spinner background color

androidandroid-layoutandroid-spinner

提问by Crishnan Iyengar

I'm using three spinners inside of my XML file. Want to change spinner color until press the next spinner.

我在我的 XML 文件中使用了三个微调器。想要更改微调器颜色,直到按下下一个微调器。

This is my xml I used:

这是我使用的xml:

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

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

and this is mybg.xml

这是 mybg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@style/AppBaseTheme.Yellow"/>
    <item android:state_selected="true" android:drawable="@style/AppBaseTheme.Yellow" />
</selector>

And Style :

和风格:

<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">
    </style>
    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
    </style>
    <style name="AppBaseTheme.Yellow">
    <item name="android:background">#FFAA00</item>
</style> 
</resources>

回答by Gustavo

You can change mybg.xml as below.

您可以更改 mybg.xml 如下。

<?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>

If want to show the arrow (">"). You can change your file mybg.xml as below. The nine-patch file can be found in /Android/android-sdks/plataforms//data/res/spinner_default_holo_light.9.png. Copy this to your drawable folder.

如果要显示箭头(“>”)。您可以按如下方式更改文件 mybg.xml。九个补丁文件可以在/Android/android-sdks/plataforms//data/res/spinner_default_holo_light.9.png 中找到。将此复制到您的可绘制文件夹。

File res/drawable/mybg.xml

文件 res/drawable/mybg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="transparent">
    <item
        android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#AAFFAA00"/>
        </shape>
    </item>
    <item 
        android:state_selected="true">
        <shape android:shape="rectangle">
            <solid android:color="#AAFFAA00"/>
        </shape>
    </item>
    <item android:drawable="@drawable/spinner_default_holo_light"></item>
</layer-list>

File res/layout/activity_main

文件 res/layout/activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="38dp"
    android:layout_toRightOf="@+id/textView1"
    android:entries="@array/listX"/>

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="119dp"
    android:layout_toRightOf="@+id/textView1"
    android:entries="@array/listX"/>

<Spinner
    android:id="@+id/spinner3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="151dp"
    android:layout_toRightOf="@+id/textView1"
    android:entries="@array/listX"/>

File MainActivity.java

文件 MainActivity.java

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Spinner;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Spinner sp1, sp2, sp3;


        sp1 = (Spinner)findViewById(R.id.spinner1);
        sp2 = (Spinner)findViewById(R.id.spinner2);
        sp3 = (Spinner)findViewById(R.id.spinner3);

        Drawable d = sp1.getBackground();

        sp1.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                sp1.setBackgroundResource(R.drawable.mybg);
                sp2.setBackgroundResource(R.drawable.spinner_default_holo_light);
                sp3.setBackgroundResource(R.drawable.spinner_default_holo_light);
                return false;
            }
        });

        sp2.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                sp1.setBackgroundResource(R.drawable.spinner_default_holo_light);
                sp2.setBackgroundResource(R.drawable.mybg);
                sp3.setBackgroundResource(R.drawable.spinner_default_holo_light);
                return false;
            }
        });

        sp3.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                sp1.setBackgroundResource(R.drawable.spinner_default_holo_light);
                sp2.setBackgroundResource(R.drawable.spinner_default_holo_light);
                sp3.setBackgroundResource(R.drawable.mybg);
                return false;
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

回答by Nashe

Inspired by Gustavo's answer, here is my version of Spinner's background with DropDown arrow. Just the complete background, not only the arrow.

受古斯塔沃的回答启发,这是我的带有下拉箭头的 Spinner 背景版本。只是完整的背景,而不仅仅是箭头。

This is how it looks

这是它的样子

Screenshot of Spinner using spinner_bg.xml

使用 spinner_bg.xml 的 Spinner 屏幕截图

Apply on spinner like

涂抹在微调器上

<Spinner
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@drawable/spinner_bg" />

spinner_bg.xml

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/InputBg" />
</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="#666666" />
        <stroke android:color="#aaaaaa" android:width="1dp"/>
    </shape>
</rotate>
</item>
<item android:width="30dp" android:height="10dp" android:bottom="21dp" android:gravity="center">
<shape android:shape="rectangle">
    <solid android:color="@color/InputBg"/>
</shape>
</item>
</layer-list>
</item>
</layer-list>

@color/InputBgshould be replaced by the color you want as your background.

@color/InputBg应该替换为您想要作为背景的颜色。

First it fills the background with desired color. Then a child layer-list makes a square and rotates it by 45 degrees and then a second Rectangle with background color covers the top part of rotated square making it look like a down arrow. (There is an extra stroke in rotated rectangle with is not really required)

首先,它用所需的颜色填充背景。然后子图层列表制作一个正方形并将其旋转 45 度,然后第二个带有背景颜色的矩形覆盖旋转正方形的顶部,使其看起来像一个向下的箭头。(旋转矩形中有一个额外的笔划,实际上并不需要)