android - 如何更改 Android 微调器文本颜色?

标签 android android-layout android-studio android-spinner

我知道这个问题已经被问了很多(参见:How to customize a Spinner in AndroidHow to change layout of spinner in Android , How to change spinner text size and text color? , How to change spinner text color )

所有答案都建议创建一个 custom_spinner.xml 文件来完成此任务。这个文件必须是这样的:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:singleLine="true"
android:textColor="@color/#FFFFFF" />

但是,没有人说出该文件的位置。在这个答案中 https://stackoverflow.com/a/37859442/5616178说这个文件必须在 Drawable 文件夹中,但是当我这样做时,Android Studio 会引发“Element TextView must be declared”错误。当我在布局文件夹中找到它时,R.layout 也不会解析它。

感谢您的回答。

编辑

我能够解决问题。正如你们许多人所说,该文件必须位于 res/layout 中,因为它是布局资源。一开始,我的代码是这样的:

citiesSpinner.setAdapter(
                    new ArrayAdapter<String>(SignUp.this,
                    android.R.layout.custom_spinner,
                    cityNames)
);

当我在构造函数外声明适配器时,即:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    SignUp.this,
                    R.layout.custom_spinner,
                    cityNames);
citiesSpinner.setAdapter(adapter);

R 类能够解析布局。我不知道为什么会这样,如果有人能解释一下会很有用。再次感谢您的回答!

最佳答案

However, no one says where to locate this file.

那是布局资源。默认情况下,它将进入 res/layout/

When I locate it in the layout folder, the R.layout won't resolve it either.

那你还有其他问题。例如,您的布局资源中可能存在阻止重新生成 R 类的错误。

关于android - 如何更改 Android 微调器文本颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39880078/

相关文章:

android - 如何强制 Android Studio 使用相对路径?

android - 将 Bluestacks 连接到 Android Studio

android - 无法启动 AVD 和 SDK 管理器 (Android Studio 0.2.1)

android - 为什么我们的 Android 2.2 应用程序 "incompatible"与 Android 2.3.5

Android:RadioGroup 下嵌套布局中的 RadioButton 脱离组

android - 这是后退按钮的正确使用方法吗

android - 如何在android中截取布局的屏幕截图

android - 如何通过 XML Drawable - Android 绘制圆圈?

java - 为什么 Android 监听器通常在 Activity 中实现?

Androidx 首选项不包括铃声首选项