Атрибут textColor не работает. Вот мой XML:
<PreferenceCategory
android:title="Title"
android:textColor="#00FF00">
Любые идеи?
Атрибут 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" />
Одним из решений является создание темы для вашего экрана предпочтений. Итак, в ваших theme.xml или styles.xml(лучше поместить его в theme.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:
<PreferenceCategory
android:layout="@layout/preferences_category"
android:title="Privacy" >
Затем установите свой код внутри вашего макета файла 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"/>
Чтобы изменить цвет текста в категории предпочтений, установите тему на PreferenceActivity
в вашем манифесте Android и убедитесь, что существует элемент colorAccent. Этот цвет используется вашим PreferenceCategory
.
Другим способом будет упоминание темы в вашей 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 Настройки res
Важно: я использую PreferenceFragmentCompat из lib v7 Preference.
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 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>
Или вы также можете использовать этот ответ
Собственно, только что узнал этот текст категории предпочтений, используя colorAccent
.
Если ваше приложение не использовало стиль colorAccent
, вы можете перейти на styles.xml
и найти <item name="colorAccent">@color/colorPrimary</item>
и изменить цвет по своему желанию.
Определите свою собственную 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">