将自定义样式应用于微调器

时间:2016-04-17 17:10:04

标签: android android-layout android-spinner

我正在尝试在微调器上应用自定义样式:

Spinner classClassification = (Spinner) findViewById(R.id.classClassification);
Resources res = getResources();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.spinner_custom, res.getStringArray(classArrayResource));
arrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_layout);
classClassification.setAdapter(arrayAdapter);

in spinner_custom:

<?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:ellipsize="marquee"
    android:textAlignment="inherit"/>

in spinner_dropdown_layout:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="@style/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dimen_5"
    android:ellipsize="marquee" />

styles

<!--Spinner Style-->
<style name="spinnerItemStyle" parent="android:Widget.TextView.SpinnerItem">
    <item name="android:layout_height">40sp</item>
    <item name="android:textSize">55sp</item>
</style>

<style name="spinnerDropDownItemStyle" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
    <item name="android:layout_height">40sp</item>
    <item name="android:textSize">55sp</item>
</style>

但上述结果没有变化,我做错了什么?

1 个答案:

答案 0 :(得分:0)

我最好的猜测是问题出在spinner_custom的TextView中。您应该将样式设置为@ style / spinnerDropDownItemStyle

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style:="@style/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"/>