我使用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>
values / styles.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);
}
}
最佳答案
我的问题的目的是了解为什么会发生这种情况。我不想手动设置黑色。
原因是您的应用程序主题(AppTheme)具有普通Spinners的已定义样式,但是CustomSpinner确实适合该类别。要解决该问题,请按照zgc的说明进行操作或检查以下内容:How to: Define theme (style) item for custom widget
关于android - 为什么自定义微调器的样式与默认样式不同?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28528413/