简体   繁体   English

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

[英]Applying a custom style to a spinner

I am trying to apply a custom style over a 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" />

And in styles : 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>

But no change in above takes effect in the outcome, what am I doing wrong? 但是上面的变化没有对结果产生影响,我做错了什么?

My best guess is that the problem is in the TextView in spinner_custom. 我最好的猜测是问题出在spinner_custom的TextView中。 You should set the style equal to @style/spinnerDropDownItemStyle 您应该将样式设置为@ 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"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM