Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 设置选定内容上微调器项目的背景色_Android_Android Layout - Fatal编程技术网

Android 设置选定内容上微调器项目的背景色

Android 设置选定内容上微调器项目的背景色,android,android-layout,Android,Android Layout,我正在代码中使用控件。我希望在选择时突出显示该项目(即,该项目的背景色已更改)。如何实现这一点?为ex:mybg.xml创建一个xml:for <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false" android:drawable="

我正在代码中使用控件。我希望在选择时突出显示该项目(即,该项目的背景色已更改)。如何实现这一点?

为ex:mybg.xml创建一个xml:for

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@color/anyColor" />
<item android:drawable="@android:color/transparent" />
</selector>

在您的活动xml中


  • 创建自定义视图布局(例如,从TextView)
  • 创建选择器并将其设置为该视图的背景
  • 使用自定义视图设置微调器
  • 选择器:custom_Selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true" 
              android:state_pressed="false" 
              android:drawable="@color/light_grey" />
        <item android:state_focused="true" 
              android:state_pressed="true"
              android:drawable="@color/light_grey" />
        <item android:state_focused="false" 
              android:state_pressed="true"
          android:drawable="@color/light_grey" />
        <item android:state_selected="true" android:drawable="@color/light_grey"/>
        <item android:drawable="@color/white" />
    </selector>
    
    
    
    自定义视图布局:我的\u简单\u项目

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:lines="1"
    android:padding="5dip"
    android:background="@drawable/custom_selector"/>
    
    
    
    初始化微调器:

    String[] items = new String[] {"One", "Two", "Three"};
    Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_simple_item, items);
    
    String[]items=新字符串[]{“一”、“二”、“三”};
    微调器微调器=(微调器)findViewById(R.id.mySpinner);
    ArrayAdapter=新的ArrayAdapter(此,R.layout.my_simple_项,项);
    

    希望这有助于

    使用鼠标滚轮滚动时没有任何效果。项目应为浅灰色,但应为橙色(android 2.2的默认颜色),请参见我的问题
    String[] items = new String[] {"One", "Two", "Three"};
    Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_simple_item, items);