自定义首选项类别标题

Jos*_*osh 31 android preferenceactivity android-preferences android-layout

我有一个像这样定义的简单首选项屏幕

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Security">
        <CheckBoxPreference 
            android:title="Require Pin on Start"
            android:summary="Require pin to run the application"
            android:key="@string/pref_require_pin"
            android:defaultValue="false" />
    </PreferenceCategory>

    <PreferenceCategory android:title="Settings">
        <ListPreference
           android:title="History Age (in days)"
           android:summary="Display items up to 30 days old"
           android:key="@string/pref_history_days"
           android:defaultValue="30"
           android:entries="@array/days_list"
           android:entryValues="@array/days_list"
           android:dialogTitle="Select History Age"/>
    </PreferenceCategory>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

我已经有一个样式设置,并在我的应用程序的其他地方使用

<style name="ListHeader">
    <item name="android:textColor">#000000</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">12sp</item>
    <item name="android:background">#cccccc</item>
    <item name="android:paddingTop">6px</item>
    <item name="android:paddingBottom">6px</item>
    <item name="android:paddingLeft">12px</item>
</style>
Run Code Online (Sandbox Code Playgroud)

这是我的活动

public class PreferencesActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.layout.preferences);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何将自定义样式应用于PreferenceCategory标题?

ina*_*ruk 57

你应该看看Preference.Category风格:

<style name="Preference.Category">
    <item name="android:layout">@android:layout/preference_category</item>
   <item name="android:shouldDisableView">false</item>
   <item name="android:selectable">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我们来看看preference_category.xml文件:

<!-- Layout used for PreferenceCategory in a PreferenceActivity. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/listSeparatorTextViewStyle"
    android:id="@+android:id/title"
/>
Run Code Online (Sandbox Code Playgroud)

因此,您需要创建自定义主题,扩展默认的android ThemelistSeparatorTextViewStyle使用ListHeader样式覆盖值.然后将此主题应用于扩展的Activity PreferenceActivity.


这是你如何做到的.

首先,在您styles.xml添加下一个代码中:

<style name="PreferenceListHeader" 
       parent="@android:style/Widget.TextView.ListSeparator">

    <item name="android:textColor">#000000</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">12sp</item>
    <item name="android:background">#cccccc</item>
    <item name="android:paddingTop">6px</item>
    <item name="android:paddingBottom">6px</item>
    <item name="android:paddingLeft">12px</item>
</style>

<style name="Theme.Custom" parent="@android:style/Theme">
    <item name="android:listSeparatorTextViewStyle">@style/PreferenceListHeader</item>               
</style>
Run Code Online (Sandbox Code Playgroud)

然后在您的AndroidManifest.xml添加主题中添加您的偏好:

 <activity android:name=".MyPreferencesActivity" 
           android:theme="@style/Theme.Custom" 
           ... >
 ...
 </activity>
Run Code Online (Sandbox Code Playgroud)

这是一个截图:

在此输入图像描述

  • 在"@android:style/Widget.TextView.ListSeparator"中遇到了一些问题,所以我将其替换为"@android:attr/listSeparatorTextViewStyle".不要忘记定义layout_height和layout_width. (9认同)
  • "检索项目的父项时出错:找不到与给定名称匹配的资源@android:style/Widget.TextView.ListSeparator" (6认同)
  • 您不必使用样式,只需在PreferenceCategory上使用android:layout属性即可 (3认同)
  • 这个答案不再适用了.android:style/Widget.TextView.ListSeparator不存在,并且android:attr/listSeparatorTextViewStyle"也不存在.如果你试图在这个解决方案中完全做什么,但选择任何现有的样式来进行编译,应用程序只是崩溃.我不敢相信这是如此混乱. (2认同)

Roh*_*wal 20

@inazaruk给出了足够好的答案但是从最近的更新开始,给出错误ADT 18 and above有一些限制styles

Error retrieving parent for item: No resource found that matches the given name '@android:style/Widget.TextView.ListSeparator'.

有关问题和解决方案的原因,请参阅此链接.由于这篇文章没有提供理解代码,我在这里提供我的代码

 <style name="Widget.TextView.ListSeparator" parent="@android:style/Widget.TextView">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">14sp</item>
    <item name="android:gravity">center_vertical</item>
</style>

<style name="PreferenceListHeader" parent="Widget.TextView.ListSeparator">
    <item name="android:textColor">#000000</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">18sp</item>
    <item name="android:background">#cccccc</item>
    <item name="android:paddingTop">6dp</item>
    <item name="android:paddingBottom">6dp</item>
    <item name="android:paddingLeft">12dp</item>
</style>

<style name="PreferenceScreen" parent="android:Theme.NoTitleBar">
    <item name="android:listSeparatorTextViewStyle">@style/PreferenceListHeader</item>
    <item name="android:background">#F2B1DBF3</item>
</style>
Run Code Online (Sandbox Code Playgroud)

  • 感谢更新.我刚刚遇到API级别19的确切问题! (2认同)
  • 请注意Lollipop为首选项和类别使用不同的样式.看看https://github.com/android/platform_frameworks_base/blob/lollipop-mr1-release/core/res/res/values/styles_material.xml#L57 (2认同)

Gáb*_*bor 15

按照inazaruk的回答描述它的样式很简单,但它只改变了标题中文本的样式,它没有提供指定全新布局的方法(样式不会应用该layout项目).但是,如果扩展课程,则有一个简单的解决方案:

public class MyPreferenceCategory extends PreferenceCategory {

  public MyPreferenceCategory(Context context) {
    super(context);
    setLayoutResource(R.layout.yourlayout);
  }

  public MyPreferenceCategory(Context context, AttributeSet attrs) {
    super(context, attrs);
    setLayoutResource(R.layout.yourlayout);
  }
}
Run Code Online (Sandbox Code Playgroud)

PreferenceCategory在定义Preferences布局时,只需使用它而不是原始.

当然,您的布局可以包含您喜欢的任何内容,包括文本上方或下方的线条,不同的背景,填充等等.例如,这将显示一个带有上面一行的彩色字幕的材料设计:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="6dp"
        android:background="?attr/divider_color" />
    <TextView
        android:id="@+android:id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="12dp"
        android:textColor="?attr/colorAccent"
        android:textSize="14sp"
        android:textStyle="bold" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


Ali*_*.DM 10

就像Gábor的答案一样,但不是扩展PreferenceCategory,你可以这样做:1.制作你的自定义布局.我将它命名为preference_category.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginTop="6dp"
    android:background="?attr/divider_color" />
<TextView
    android:id="@+android:id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:padding="12dp"
    android:textColor="?attr/colorAccent"
    android:textSize="14sp"
    android:textStyle="bold" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

重要提示:布局必须包含带有此id的textview:android:id ="@ + android:id/title"

2.并在首选项中添加以下行:android:layout ="@ layout/preference_category"

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<PreferenceCategory
    android:key="Font_Settings"
    android:title="@string/UISetting"
    android:layout="@layout/preference_category" >
... //  other preferences in the category
</PreferenceCategory>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)