在我的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"

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


当前回答

Ashraf的另一个解决方案是确保你考虑到屏幕尺寸。你需要在onCreate中获得旋转器,并在设置适配器后设置监听器:

//set your adapter with default or custom spinner cell, then://
serverSpinner.setOnItemSelectedListener(spinnerSelector);
serverSpinner.setSelection(defaultServer);

然后你可以开始改变在旋转器被点击之前显示的视图的文本大小:

private AdapterView.OnItemSelectedListener spinnerSelector = new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
        boolean largeTablet = getResources().getBoolean(R.bool.isLargeTablet);
        if (tabletSize) { ((TextView)parent.getChildAt(0)).setTextSize(16); }
        else if (largeTablet) { ((TextView)parent.getChildAt(0)).setTextSize(18); }
        else { ((TextView)parent.getChildAt(0)).setTextSize(12); }
    }
    public void onNothingSelected(AdapterView<?> parent) {

    }
};

你所需要做的就是创建如下布局的特定文件夹:

values-sw360dp values-sw600dp values-sw800dp

然后在每个文件夹中添加一个名为“bol . XML”的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="isTablet">false</bool>
    <bool name="isLargeTablet">false</bool>
</resources>

其他回答

重用/更改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);
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:ellipsize="marquee"
    android:textAlignment="inherit"/>

就用这个吧:

ArrayAdapter<String> adapter_category = new ArrayAdapter<String>(this,
    R.layout.spinner_list_item, categories);
adapter_category
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Ashraf的另一个解决方案是确保你考虑到屏幕尺寸。你需要在onCreate中获得旋转器,并在设置适配器后设置监听器:

//set your adapter with default or custom spinner cell, then://
serverSpinner.setOnItemSelectedListener(spinnerSelector);
serverSpinner.setSelection(defaultServer);

然后你可以开始改变在旋转器被点击之前显示的视图的文本大小:

private AdapterView.OnItemSelectedListener spinnerSelector = new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
        boolean largeTablet = getResources().getBoolean(R.bool.isLargeTablet);
        if (tabletSize) { ((TextView)parent.getChildAt(0)).setTextSize(16); }
        else if (largeTablet) { ((TextView)parent.getChildAt(0)).setTextSize(18); }
        else { ((TextView)parent.getChildAt(0)).setTextSize(12); }
    }
    public void onNothingSelected(AdapterView<?> parent) {

    }
};

你所需要做的就是创建如下布局的特定文件夹:

values-sw360dp values-sw600dp values-sw800dp

然后在每个文件夹中添加一个名为“bol . XML”的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="isTablet">false</bool>
    <bool name="isLargeTablet">false</bool>
</resources>
    String typeroutes[] = {"Select","Direct","Non Stop"};
    Spinner typeroute;

    typeroute = view.findViewById(R.id.typeroute);

    final ArrayAdapter<String> arrayAdapter5 = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_spinner_item, typeroutes) {
            @Override
            public boolean isEnabled(int position) {
                if (position == 0) {
                    // Disable the first item from Spinner
                    // First item will be use for hint
                    return false;
                } else {
                    return true;
                }
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                View v = super.getView(position, convertView, parent);
                ((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
                ((TextView) v).setTextColor(Color.parseColor("#ffffff"));
                return v;
            }         ---->in this line very important so add this

            @Override
            public View getDropDownView(int position, View convertView,
                                        ViewGroup parent) {
                View view = super.getDropDownView(position, convertView, parent);
                TextView tv = (TextView) view;
                if (position == 0) {
                    // Set the hint text color gray
                    tv.setTextColor(Color.GRAY);
                } else {
                    tv.setTextColor(Color.BLACK);
                }
                return view;
            }
        };

        arrayAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        typeroute.setAdapter(arrayAdapter5);

这就是所有的享受你的编码…

只需添加新的样式,像这样:

<style name="mySpinnerItemStyle" parent="ThemeOverlay.AppCompat.Dark">
    <item name="android:textColor">#000</item>
    <item name="android:color">#000</item>
</style>

并使用它:

<Spinner
      android:id="@+id/spinnerCategories"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      style="@style/mySpinnerItemStyle"
      android:layout_margin="5dp" />