[英]How to change text color of preference category in Android?
textColor 屬性不起作用。 這是我的 XML:
<PreferenceCategory
android:title="Title"
android:textColor="#00FF00">
有任何想法嗎?
使用此自定義 PreferenceCategory 類:
public class MyPreferenceCategory extends PreferenceCategory {
public MyPreferenceCategory(Context context) {
super(context);
}
public MyPreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyPreferenceCategory(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
TextView titleView = (TextView) view.findViewById(android.R.id.title);
titleView.setTextColor(Color.RED);
}
}
並將其添加到您的 Pref.xml 文件中:
<ali.UI.Customize.MyPreferenceCategory android:title="@string/pref_server" />
一種解決方案是為您的 PreferenceScreen 制作一個主題。 所以在你的themes.xml 或styles.xml(最好把它放在themes.xml 中):
<style name="PreferenceScreen" parent="YourApplicationThemeOrNone">
<item name="android:textColor">@color/yourCategoryTitleColor</item>
</style>
然后在你的 AndroidManifest.xml 中:
<activity
android:name="MyPreferenceActivity"
...
android:theme="@style/PreferenceScreen" >
</activity>
它非常適合我。
一種簡單的方法是在此處為首選項類別設置自定義布局:
<PreferenceCategory
android:layout="@layout/preferences_category"
android:title="Privacy" >
然后在您的首選項_類別布局文件中設置您的代碼:
<TextView
android:id="@android:id/title"
android:textColor="@color/deep_orange_500"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textAllCaps="true"/>
要更改首選項類別的文本顏色,只需在您的 Android 清單中為您的PreferenceActivity
設置一個主題,並確保存在colorAccent項目。 此顏色由您的PreferenceCategory
。
實際上只是使用colorAccent
發現了偏好類別文本。
如果您的應用程序沒有使用colorAccent
樣式,您可以去styles.xml
並找到
<item name="colorAccent">@color/colorPrimary</item>
,並根據需要更改顏色。
其他方式將在您的AppTheme 中提及主題,應用程序級別
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
.....//your other items
<item name="preferenceTheme">@style/PrefTheme</item>
</style>
<style name="PrefTheme" parent="@style/PreferenceThemeOverlay">
<item name="preferenceCategoryStyle">@style/CategoryStyle</item>
</style>
<style name="CategoryStyle" parent="Preference.Category">
<item name="android:layout">@layout/pref_category_view</item>
</style>
XML : pref_category_view
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@android:id/title"
style="?android:attr/listSeparatorTextViewStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:textColor="@color/red"
android:layout_height="wrap_content"
/>
如需更多定制,請訪問v7 Preferences res
重要提示:我使用PreferenceFragmentCompat從LIB V7偏好。
使用 Material Theme,您只需覆蓋以下屬性:
<style name="YourTheme" parent="@style/Theme.MaterialComponents">
<item name="colorAccent">@color/your_custom_color</item>
</style>
public class MyPreferenceCategory extends PreferenceCategory {
public MyPreferenceCategory(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyPreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyPreferenceCategory(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
protected View onCreateView(ViewGroup parent) {
// It's just a TextView!
TextView categoryTitle = (TextView)super.onCreateView(parent);
categoryTitle.setTextColor(parent.getResources().getColor(R.color.orange));
return categoryTitle;
}
}
在您的 prefs.xml 中:
<com.your.packagename.MyPreferenceCategory android:title="General">
.
.
.
</com.your.packagename.MyPreferenceCategory>
或者你也可以使用這個答案
定義您自己的 PreferenceTheme 並覆蓋顏色。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="preferenceTheme">@style/AppTheme.PreferenceTheme</item>
</style>
<style name="AppTheme.PreferenceTheme" parent="PreferenceThemeOverlay.v14.Material">
<item name="colorAccent">`#color_value`</item>
</style>
受到@AliSh 答案的啟發,但我只需要更改一個Preference
文本項的顏色。 所以,對於所有的 Kotlin 人來說:
class TextColorPreference : Preference {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(
context: Context, attrs: AttributeSet,
defStyle: Int
) : super(context, attrs, defStyle)
override fun onBindViewHolder(holder: PreferenceViewHolder?) {
super.onBindViewHolder(holder)
context?.let {
(holder?.findViewById(android.R.id.title) as? TextView)?.setTextColor(
ContextCompat.getColor(
it,
R.color.colorPrimary
)
)
}
}
}
然后把它放在你的xml/prefs.xml
:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<this.should.be.your.package.TextColorPreference
android:id="@+id/settings_logout"
android:key="@string/prefs_key_logout"
android:title="@string/settings_logout" />
</PreferenceScreen>
許多其他答案對我的情況不起作用。 我正在使用 PreferenceFragmentCompat 並且我不想讓實際代碼這樣做。 所以我只是復制了首選項類別 xml 文件並更改了 textColor 字段。 該文件遵循 Apache 許可,版本 2。
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2015 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginStart="?android:attr/listPreferredItemPaddingLeft"
android:orientation="vertical">
<TextView
android:id="@android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingEnd="?android:attr/listPreferredItemPaddingRight"
android:textAlignment="viewStart"
android:textColor="@color/app_accent"
android:textStyle="bold"
tools:ignore="RtlSymmetry"/>
<TextView
android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textColor="?android:attr/textColorSecondary"/>
</LinearLayout>
並將其導入我的 .xml 布局中的首選項片段:
<PreferenceCategory
android:layout="@layout/preference_category_companion"
android:key="my_preference_title"
android:title="@string/my_preference_title">
對於 androidx 首選項庫,您可以像這樣擴展PreferenceCategory
或Preference
類:
class DangerPreference(
context: Context?,
attrs: AttributeSet?,
): Preference(context, attrs) {
override fun onBindViewHolder(holder: PreferenceViewHolder?) {
super.onBindViewHolder(holder)
holder?.itemView?.findViewById<TextView>(android.R.id.title)?.setTextColor(Color.RED)
}
}
然后在您的PreferenceScreen
正常使用它:
<com.github.anastr.myscore.util.pref.DangerPreference
app:key="deleteServerData"
app:title="@string/delete_server_data"
app:iconSpaceReserved="false" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.