首选项屏幕显示文本块

时间:2012-08-09 13:22:12

标签: android android-preferences preferencescreen

我正在尝试创建一个只有关联,联系和合法选项的首选项屏幕,所有这些选项在单击时只显示单独页面中的文本模糊和图标,不是shared preferences或其他任何内容。

我无法理解层次结构以显示文本。我希望流程为:settings -> about -> the about text

目前我有这个,它给了我类别和选项,但我不知道该怎么做才能显示新文本。

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
            android:title="Info">
        <Preference android:title="About"/>


    </PreferenceCategory>
...
</PreferenceScreen>

我不知道使用什么选项来将可点击进入文本视图。

5 个答案:

答案 0 :(得分:36)

A.Grandt的解决方案非常好地模仿了Preference Activity中的文本块,但我会添加一个android:persistent="false"属性,这样可以避免不必要地存储这个&#39;伪偏好&#39;进入应用程序设置的SharedPreferences文件。

总结一下。在A.Adrand建议的模拟Preferences活动中的TextView将是:

<Preference
    android:key="pref_static_field_key"
    android:selectable="false"
    android:persistent="false"
    android:title="you can omit the title"
    android:summary="Multi line description\ncan go here."/>

虽然您可以使用\n来换行,但它不会调整首选项本身的大小,因此这些多线可能无法在单行首选项中完全显示

答案 1 :(得分:22)

我遇到了同样的问题,需要显示静态文本块。虽然在我的情况下,它已被列入首选项页面。

标签允许链接虽然不能解决对静态文本的需求。但是,Preference标签本身也是如此。你可以让它显示文本,通常的标题行,以及下面的文本摘要,两者都是可选的,然后使其无法选择。它会给人一种静态文本的错觉。

<Preference
    android:key="pref_static_field_key"
    android:selectable="false"
    android:title="you can omit the title"
    android:summary="Multi line description\ncan go here."/>

答案 2 :(得分:11)

您无法在PreferenceScreen内添加格式化的文本块,这不是它的意思。但是,您可以在其他活动中添加“关于”文字(LinearLayout,其中某些格式化TextView可能就足够了。通过在首选项中传递一个intent来调用它:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >    
    <Preference 
        android:key="about"   
        android:title="About">           
            <intent android:action="your.package.path.to.Activity"/>           
    </Preference>
</PreferenceScreen>

答案 3 :(得分:8)

我想添加一个文本块,但以编程方式设置实际文本内容。我的具体需求:在首选项屏幕的顶部显示应用版本名称。

我按照A.GrandtKrzysiek的方法完成了这项工作,并在代码中设置了摘要。

dev_preferences.xml

<Preference
    android:key="pref_app_version" 
    android:persistent="false"
    android:selectable="false"
    android:title="@string/app_name" />

偏好片段(extends PreferenceFragment):

  private static final String KEY_APP_VERSION = "pref_app_version";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.dev_preferences);

        findPreference(KEY_APP_VERSION).setSummary(BuildConfig.VERSION_NAME);
    }

我还通过在android:layout="@layout/dev_preferences_header"条目中设置<Preference>来使用自定义布局(as in this answer)。 (不是强制性的,但通过这种方式,您可以轻松自定义首选项的外观&#34;标题&#34;。)

dev_preferences_header.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"       
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:padding="@dimen/spacing_small">

    <TextView
        android:id="@android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@android:id/summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@android:id/title" />

</RelativeLayout>

答案 4 :(得分:1)

在“设置”页面中,我想显示我的应用程序数据的上次备份日期时间。

(灵感来自: https://developer.android.com/reference/android/content/SharedPreferences.Editor

1)在我的preference.xml中:

<PreferenceCategory app:title="Backup last execution">
    <EditTextPreference
        app:key="backup"
        app:selectable="false"
        app:summary="never"
        app:useSimpleSummaryProvider="true" />
</PreferenceCategory>

2)在我的类工具{伴侣对象}中:

fun Utility.Companion.storeToPreferences(key:String, value:String){
       val sharedPref: SharedPreferences =
           PreferenceManager.getDefaultSharedPreferences(context)
       val editor = sharedPref.edit()
       editor.putString(key, value)
       editor.apply()
}

3)我的电话:

 Utility.storeToPreferences("backup", LAST_BACKUP)
相关问题