为什么自定义微调器具有与默认不同的样式?

时间:2015-02-15 16:42:52

标签: android

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

问题:默认的微调器下拉视图为黑色,但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);
    }
}

布局布局/ 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>

值/ styles.xml

<resources>

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

</resources>

的src /主/ 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 \主\的java \ COM \示例\ 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);
    }
}

1 个答案:

答案 0 :(得分:1)

  

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

原因是您的应用程序(AppTheme)的主题具有正常Spinners的定义样式,但您的CustomSpinner最适合该类别。为了解决这个问题,请按照zgc的说法进行操作或检查:How to: Define theme (style) item for custom widget