在我的Android应用程序中,我正在使用旋转器,我已经从SQLite数据库加载数据到旋转器中,它工作正常。这是它的代码。

Spinner spinner = (Spinner) this.findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>  (this,android.R.layout.simple_spinner_item, list);
cursor.moveToFirst();

list.add("All Lists");

if (cursor.getCount() > 0) {
    for (int i = 0; i < cursor.getCount(); i++) {
        keyList[i] = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.KEYWORD));
        list.add(keyList[i]);
        cursor.moveToNext();
    }
}
Database.close();
cursor.close();
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);

现在我想要改变旋转数据的文本颜色和文本大小。我在XML文件上使用了以下XML行作为旋转标记,但它不起作用。

android:textColor="@android:color/white"
android:textSize="11dp"

我如何改变我的转轮的文本颜色和文本大小?


当前回答

更改微调文本的颜色:

 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);}

其他回答

重用/更改android.R.layout资源最简单的方法就是使用定义。在Android Studio中,在Android . r.b ayout.simple_spinner_item.xml上按Ctrl + B。

它将把您带到资源文件。只需要复制资源文件,并在Package.R.layout文件夹中添加一个新的布局,并更改textview的textColor,然后像这样在适配器中调用它:

ArrayAdapter<String> adapter = new ArrayAdapter<String(Context,R.layout.spinner_item, spinnerlist);

为了防止滞后,你不仅需要在onItemSelected监听器中设置文本属性,还需要在Activity的onCreate方法中设置文本属性(但这有点棘手)。

具体来说,你需要在设置适配器后把这个放在onCreate中:

spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);

然后把这个放到onItemSelected中:

((TextView) view).setTextColor(backgroundColor);

下面是一个完整的例子:

@Override  
protected void onCreate(Bundle savedInstanceState)
{  
    Spinner spinner = (Spinner) findViewById(R.id.spinner); 

    //Set the choices on the spinner by setting the adapter.  
    spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));

    //Set the text color of the Spinner's selected view (not a drop down list view) 
    spinner.setSelection(0, true);
    View v = spinner.getSelectedView();
    ((TextView)v).setTextColor(backgroundColor);

    //Set the listener for when each option is clicked.  
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {  

        @Override  
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
        {  
           //Change the selected item's text color  
           ((TextView) view).setTextColor(backgroundColor);
        }  

        @Override  
        public void onNothingSelected(AdapterView<?> parent)
        {  
        }  
    });  

}  

要了解更多细节,请参见我的问题。

更改微调文本的颜色:

 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);}

如果您想要一个简单的方法,为了将项添加到下拉列表中,您通常将它们添加到strings.xml中。下面是一个关于如何使用strings.xml文件添加颜色的例子:

选择年龄范围

<string-array name="age_array">

   <item> 0-6 </item>                               //No custom colour

  <item><font fgcolor='#FF4CD964'> 12+ </font></item> //With custom colour

</string-array>

如果您使用的是android.support.v7.widget。AppCompatSpinner是使用样式的最简单的测试解决方案:

 <android.support.v7.widget.AppCompatSpinner
                    android:id="@+id/spefcialFx"
                    style="@style/Widget.AppCompat.Spinner.Underlined"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="4dp"
                    android:theme="@style/Spinner"
                    android:entries="@array/special_fx_arrays"
                    android:textSize="@dimen/text_size_normal"></android.support.v7.widget.AppCompatSpinner>

以及风格:

<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
        <item name="android:paddingStart">0dp</item>
        <item name="android:paddingEnd">0dp</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:backgroundTint">@color/red</item>
        <item name="android:textSize">14sp</item>
    </style>

唯一的缺点是android:backgroundTint为下拉箭头和下拉背景设置颜色。