我知道这个问题已经被问了很多(参见:How to customize a Spinner in Android, How 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/