自定义偏好类别标题
我有一个像这样定义的简单首选项屏幕,
<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>
我已经有一个样式设置并在我的应用程序的其他地方使用。
<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>
这是我的活动
public class PreferencesActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.preferences);
}
}
如何将自定义样式应用到 PreferenceCategory
标题?
I have a simple preference screen defined like this
<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>
I have a style setup already and used elsewhere in my app.
<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>
and here is my activity
public class PreferencesActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.preferences);
}
}
How do I apply my custom style to the PreferenceCategory
heading?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该看看
Preference.Category
样式:让我们看一下
preference_category.xml
文件:因此您需要创建扩展默认 android 的自定义主题
Theme
并使用ListHeader
样式覆盖listSeparatorTextViewStyle
值。然后将此主题应用于扩展PreferenceActivity
的 Activity。以下是您可以如何做到这一点。
首先,在您的
styles.xml
中添加以下代码:然后在您的
AndroidManifest.xml
中将主题添加到您的首选项活动:这是一个屏幕截图:
You should take a look at
Preference.Category
style:Let's take a look at
preference_category.xml
file:So you need to create custom theme that extends default android
Theme
and overridelistSeparatorTextViewStyle
value withListHeader
style. And then apply this theme to Activity that extendsPreferenceActivity
.Here is how you can do it.
First, in your
styles.xml
add next code:Then in your
AndroidManifest.xml
add theme to your preference acitivity:Here is a screenshot:
@inazaruk 给出了足够好的答案,但自从最近的更新(
ADT 18 及以上
)以来,对styles
存在一些限制,给出了错误Error retriving Parent for item :找不到与给定名称“@android:style/Widget.TextView.ListSeparator”匹配的资源。
请参阅此链接了解问题原因和解决方案。由于这篇文章没有提供用于理解的代码,因此我在这里提供我的代码
@inazaruk gave the answer well enough but since the recent updates,
ADT 18 and above
, there are some restrictions on thestyles
giving the errorError retrieving parent for item: No resource found that matches the given name '@android:style/Widget.TextView.ListSeparator'.
See this link for reason of the problem and the solution. Since this post doesn't provide a code for understanding i am providing my code here
按照 inazaruk 的答案中描述的方式对其进行样式设置非常简单,但它仅更改标题中文本的样式,它没有提供指定全新布局的方法(该样式不会应用
layout
项目)。然而,如果您扩展该类,则有一个简单的解决方案:并在定义首选项布局时简单地使用它而不是原始的
PreferenceCategory
。当然,您的布局可以有您喜欢的任何内容,包括文本上方或下方的线条、不同的背景、填充等。例如,这将显示一个材料设计彩色字幕,上面有一行:
Styling it as described in inazaruk's answer is simple enough but it only changes the style of the text in the heading, it doesn't offer a way to specify a whole new layout (the style will not apply the
layout
item). There is, however, a straightforward solution if you extend the class:and simply use this instead of the original
PreferenceCategory
when defining your Preferences layout.Your layout can, of course, have anything you like, including lines above or below the text, different backgrounds, padding, whatever. For instance, this will show a material design colored subtitle with a line above:
就像 Gábor 的答案一样,但您可以这样做,而不是扩展 PreferenceCategory :
1. 制作自定义布局。我将其命名为 preference_category.xml :
重要:布局必须包含具有以下 id 的文本视图: android:id="@+android:id/title"< /strong>
2.并在首选项中添加此行: android:layout="@layout/preference_category"
Like Gábor's answer but instead of extending PreferenceCategory you can do like this :
1. Make your custom layout.I named it preference_category.xml :
Important : the layout must contain a textview with this id : android:id="@+android:id/title"
2.and in the preference add this line : android:layout="@layout/preference_category"