安卓:需要改变微调的背景颜色 [英] Android: Need to change the spinner background color

查看:118
本文介绍了安卓:需要改变微调的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的XML文件到三纱厂。想改变微调的颜色,直到preSS下一个微调。

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,

    <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" 
            />

这是mybg.xml

and this is 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>

和风格:

    

</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">

</style>

<style name="AppBaseTheme.Yellow">
   <item name="android:background">#FFAA00</item>

推荐答案

您可以更改mybg.xml如下图所示。

You can change mybg.xml as below.

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

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

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.

文件RES /绘制/ mybg.xml

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

文件RES /布局/ activity_main

File 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"/>

文件MainActivity.java

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

}

这篇关于安卓:需要改变微调的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆