您需要像这样显式定义 EditTextPreference 的样式:
<style name="SettingsTheme" parent="@style/AppBaseTheme">
<item name="android:editTextPreferenceStyle">@style/MyStyle</item>
</style>
<style name="MyStyle" parent="@android:style/Preference.DialogPreference.EditTextPreference">
...
</style>
这会将您在 MyStyle 中定义的任何样式仅设置为之前 EditTextPreference 默认样式的位置。
我最近也遇到了同样的问题。刚刚找到了答案。我制作了一个自定义的 EditTextPreference 并在我的代码中执行此操作来更改标题和摘要颜色,我还尝试更改图标色调(颜色)并且它正在工作,您所需要做的就是找到您需要更改的视图并应用您的视图修改如下:
class MyEditTextPreference : EditTextPreference {
constructor(context: Context) : super(context)
constructor(context: Context, attr : AttributeSet?) : super(context, attr)
constructor(context: Context, attr: AttributeSet?, i1: Int) : super(context, attr, i1)
constructor(context: Context, attr: AttributeSet?, i1: Int, i2: Int) : super(context, attr, i1, i2)
override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
(holder.findViewById(android.R.id.title) as TextView).setTextColor(Color.WHITE)
(holder.findViewById(android.R.id.icon) as PreferenceImageView).imageTintList = ColorStateList.valueOf(Color.WHITE)
(holder.findViewById(android.R.id.summary) as TextView).setTextColor(ResourcesCompat.getColor(context.resources, R.color.test_color, null))
}
}
在你的 xml 文件中你必须使用这个
<mypackage.MyEditTextPreference
app:icon="@drawable/ic_test"
app:key="testKey"
app:title="@string/testString"
app:useSimpleSummaryProvider="true" />
注1: 我使用了一个简单的摘要提供程序,但您可以使用您想要的任何摘要。
注2: 这些代码是用 kotlin 编写的,但您可以简单地将它们转换为 Java。
我认为最简单的方法是子类化
EditTextPreference
并在 onCreateView()
或 onBindView()
中调整标题颜色。
public class MyEditTextPreference extends EditTextPreference {
// constructors omitted
@Override
protected void onBindView(View view) {
TextView titleView = (TextView) view.findViewById(android.R.id.title);
int color = getContext().getResources().getColor(R.color.preference_title);
titleView.setTextColor(color);
}
}
然后在您的首选项 XML 中,您将使用您的类(完全限定名称):
<com.package.MyEditTextPreference
android:key="username"
android:title="Your Name"
android:summary="Please provide your username." />