【问题标题】:Why custom spinner has different style than default?为什么自定义微调器的样式与默认样式不同?
【发布时间】:2015-02-15 16:42:52
【问题描述】:

我使用 android.widget.Spinner 作为父级创建了自定义 Spinner(称为 CustomSpinner)。我的 CustomSpinner 只是重写了构造函数。

问题:默认 Spinner 下拉视图为黑色,但 CustomSpinner 下拉视图在同一活动中为白色。

我应该向我的 CustomSpinner 添加什么以遵循默认的 Spinner 样式?

CustomSpinner 类:

package com.example.customspinner;

public class CustomSpinner extends Spinner {
    public CustomSpinner(Context context) {
        super(context);
    }

    public CustomSpinner(Context context, int mode) {
        super(context, mode);
    }

    public CustomSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) {
        super(context, attrs, defStyleAttr, mode);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, int mode) {
        super(context, attrs, defStyleAttr, defStyleRes, mode);
    }
}

在布局layout/activity_main.xml中的使用

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <Spinner
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinner1"
            />

    <com.example.customspinner.CustomSpinner
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinner2"
            />

</LinearLayout>

值/样式.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat">
        <!-- Customize your theme here. -->
    </style>

</resources>

src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.igorok.customspinner" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

\src\main\java\com\example\customspinner\MainActivity.java

public class MainActivity extends ActionBarActivity {

    private Spinner mSpinner;

    private static String[] mStrings = new String[] {
        "1111111",
        "2222222",
        "3333333",
        "4444444",
        "5555555"
    };

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

        mSpinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                this,
                android.R.layout.simple_spinner_dropdown_item,
                mStrings
        );
        mSpinner.setAdapter(adapter);
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

  • 设置CustomSpinner的背景为@andorid:color/black
  • @zgc7009 我的问题的目标是了解它为什么会发生。我不想手动设置黑色。谢谢。
  • 发生这种情况是因为您基本是在夸大自定义视图,这意味着它不会采用其主题的属性。

标签: android


【解决方案1】:

我的问题的目的是了解它为什么会发生。我不想手动设置黑色。

原因是您的应用程序主题 (AppTheme) 为普通 Spinner 定义了样式,但您的 CustomSpinner 不适合该类别。按照 zgc 的说法解决它或检查一下:How to: Define theme (style) item for custom widget

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    相关资源
    最近更新 更多