Thuộc tính textColor không hoạt động. Đây là XML của tôi:Làm cách nào để thay đổi màu văn bản của danh mục tùy chọn trong Android?
<PreferenceCategory
android:title="Title"
android:textColor="#00FF00">
Bất kỳ ý tưởng nào?
Thuộc tính textColor không hoạt động. Đây là XML của tôi:Làm cách nào để thay đổi màu văn bản của danh mục tùy chọn trong Android?
<PreferenceCategory
android:title="Title"
android:textColor="#00FF00">
Bất kỳ ý tưởng nào?
sử dụng tùy chỉnh lớp PreferenceCategory này:
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);
}
}
và thêm này vào tập tin Pref.xml của bạn:
<ali.UI.Customize.MyPreferenceCategory android:title="@string/pref_server" />
Một giải pháp là để thực hiện một chủ đề cho PreferenceScreen của bạn. Vì vậy, trong themes.xml hoặc styles.xml của bạn (tốt hơn để đặt nó trong themes.xml):
<style name="PreferenceScreen" parent="YourApplicationThemeOrNone">
<item name="android:textColor">@color/yourCategoryTitleColor</item>
</style>
sau đó trong AndroidManifest.xml của bạn:
<activity
android:name="MyPreferenceActivity"
...
android:theme="@style/PreferenceScreen" >
</activity>
Nó làm việc hoàn hảo đối với tôi.
Với tôi, đây là câu trả lời đúng – Javi
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;
}
}
Và trong prefs.xml của bạn:
<com.your.packagename.MyPreferenceCategory android:title="General">
.
.
.
</com.your.packagename.MyPreferenceCategory>
Hoặc bạn cũng có thể sử dụng này answer
Điều này làm việc hoàn hảo cho tôi với một sửa đổi. Thay vì 'categoryTitle.setTextColor (parent.getResources() getColor (R.color.orange).); ' tôi đã phải sử dụng ' categoryTitle.setTextColor (Color.BLACK);' – ckn
Một cách dễ dàng để làm điều này là để thiết lập cách bố trí tùy chỉnh cho preferenceCategory đây:
<PreferenceCategory
android:layout="@layout/preferences_category"
android:title="Privacy" >
Sau đó, đặt mã của bạn bên trong tệp sơ đồ preferences_category:
<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"/>
cách khác xung quanh sẽ đề cập đến các chủ đề trong bạn AppTheme, mức độ ứng dụng
<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"
/>
Để biết thêm tùy lần v7 Preferences res
quan trọng: Tôi đang sử dụng PreferenceFragmentCompat từ lib v7 Preference.
Happy_Coding;
Để thay đổi màu sắc văn bản của thể loại chọn này chỉ thiết lập một chủ đề để PreferenceActivity
của bạn trong Android Manifest của bạn và chắc chắn rằng colorAccent mục tồn tại. Màu này được chụp bởi PreferenceCategory
của bạn.
Đây là câu trả lời đúng! Thêm
Thực ra chỉ phát hiện ra rằng văn bản danh mục tùy chọn sử dụng colorAccent
.
Nếu ứng dụng của bạn không sử dụng kiểu colorAccent
, bạn có thể truy cập styles.xml
và tìm <item name="colorAccent">@color/colorPrimary</item>
và thay đổi màu như bạn muốn.
IMO, đây là câu trả lời đúng như của Lollipop và thiết kế material design. Tham khảo - http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/frameworks/base/core/res/res/layout/preference_category_material.xml? av = f – Ginandi
Tôi thấy rằng 'findViewById()' vẫn không hoạt động trong 'onBindView()'. Tôi đã làm nó trong 'onCreateView()' thay vào đó, mà làm việc. – ryan
'titleView.setTextColor (Color.RED);' ném 'NullPointerException'. Mọi sửa chữa? –
Bản sửa lỗi nằm trong chú thích đầu tiên ... – Rohan