【问题标题】:change Spinner text color更改微调器文本颜色
【发布时间】:2018-08-14 10:29:44
【问题描述】:

我想将 Spinner 中的默认文本颜色更改为任何其他颜色。但我还没有找到解决方案。我通过 TextView 得到了解决方案,例如 How to change spinner text size and text color?
Spinner 有没有其他方法,直接的方法?

注意:请提供 Xamarin 的解决方案。谢谢。

【问题讨论】:

  • 使用自定义适配器。扩展 ArrayAdapter 并覆盖 getViewgetDropDownView
  • 这个解决方案有什么问题?我猜这是最简单的。
  • @ADM 只是想知道,为什么他们为我们提供这个 Spinner,我们无法自定义,为什么?顺便说一句,我是 android 新手。
  • 保持好奇心,阅读链接中的答案。并且所有AdapterView 都是为自定义项目制作的,Spinner 也是如此。
  • 您可以编写自定义样式并添加到微调器

标签: android xamarin xamarin.forms xamarin.android android-spinner


【解决方案1】:

仅对选定的项目颜色更改这样尝试:

spinnerObject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
    ((TextView)parentView.getChildAt(0)).setTextColor(Color.RED);
}
});

另一种方式:

<style name="spinnerTheme">
<item name="android:textColor">@color/gray_dark</item>
</style>

<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="50dp"
android:theme="@style/spinnerTheme"/>

【讨论】:

  • 帮助我使用 Xamarin,谢谢。
  • 谢谢,它适用于弹出窗口文本颜色,但现在剩下的唯一问题是微调器中的默认文本颜色,即黑色。怎么改?
  • 编写自定义样式以更改文本颜色
【解决方案2】:

创建一个 Sppinner 代码

<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:textSize="20sp"
android:entries="@array/planets"/>

您需要创建自己的布局文件,其中包含微调器项目的自定义定义spinner_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ff0000" />

如果您想自定义下拉列表项,您需要创建一个新的布局文件。 spinner_dropdown_item.xml

 <?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#aa66cc"/>

最后是微调器声明的另一个变化:

 ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
 R.array.planets_array, R.layout.spinner_item);
 adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);  
 spinner.setAdapter(adapter);

【讨论】:

  • 它可能在 (Java)Android 中运行,但我想在 Xamarin 中运行。我已经尝试了两种方式 1. 放置在 Resources/drawable/ 下,扩展名为“.xml”,2. 在 Resources/layout 下,扩展名为“.axml”。我失败了。帮助我理解它。谢谢。
  • 对不起,我不检查你的标签 xamarin 我不知道 :( 我只想帮助你,但对于 java 它也可以工作
【解决方案3】:

创建一个新的 XML 布局:

<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:textColor="#962b2b"
android:textAlignment="inherit"/>

现在在自定义 XML 布局中设置微调器适配器,如下所示:

ArrayAdapter adapter = new ArrayAdapter(getActivity(),R.layout.custom_spinner,array);
    adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    spinner.setAdapter(adapter);

【讨论】:

  • 帮助我使用 Xamarin。谢谢
【解决方案4】:

您可以通过将以下代码添加到 style.xml 来做到这一点:

<resources>
<style name="MySpinnerLook" 
    parent="@android:style/Widget.TextView.SpinnerItem">
    <item name="android:textSize">22sp</item>    //any size
</style>

在使用微调器的 xml 中,使用如下样式标签添加对 MyStyleLook 的引用:

style="@style/MySpinnerLook"

【讨论】:

    【解决方案5】:
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="@style/spinnerItemStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/margin_5"
    android:ellipsize="marquee"
    android:layoutDirection="ltr"
    android:padding="@dimen/padding_5"
    android:singleLine="true"
    android:textSize="@dimen/textsize_base" />
    
    
    
    
    adapter?.setDropDownViewResource(R.layout.spinner_text)
    
    spSourceAccount.adapter = adapter
    

    【讨论】:

      猜你喜欢
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      相关资源
      最近更新 更多