【问题标题】:Preference screen display text block偏好屏幕显示文本块
【发布时间】:2012-08-09 13:22:11
【问题描述】:

我正在尝试制作一个只有关于、联系人和法律选项的首选项屏幕,当点击所有这些选项时,只会在单独的页面中显示文本简介和图标,而不是 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>

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

【问题讨论】:

    标签: android android-preferences preferencescreen


    【解决方案1】:

    A.Grandt 的解决方案在 Preference Activity 中提供了对文本块的非常好的模仿,但我会添加一个 android:persistent="false" 属性,以避免不必要地将此“伪首选项”存储到 SharedPreferences 文件中以供您的应用设置使用。

    总结一下。正如 A.Grandt 建议的那样,在 Preferences Activity 中模仿 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 进行换行,但它不会调整首选项本身的大小,因此这些多行可能不会在仍然单行的首选项中完全可见。

    【讨论】:

    • 使用这种方法,有没有办法在代码中设置文本块值(摘要)? (用例:在首选项屏幕顶部显示应用版本名称。)
    • That turned out pretty easy;在代码中,找到首选项并为其设置摘要。
    【解决方案2】:

    您不能在PreferenceScreen 中添加格式化的文本块,这不是它的本意。但是,您可以在另一个活动中添加关于文本(LinearLayout 和一些格式化的TextViews 可能就足够了)。通过在首选项中传递一个意图来调用它:

    <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>
    

    【讨论】:

    • 你能进一步扩展吗?我是否必须调用活动意图才能显示文本块?
    • 好吧,文本块本身并不意味着在偏好页面中。除非您向 about 添加摘要(仅显示未格式化的文本),否则您唯一的选择是打开一个新活动或将 about 页面定义为另一件事,而不是偏好页面
    • 好的,我明白你的意思了。我可以让它在点击时将我的偏好设置为另一个偏好,在该偏好中我使用标题和摘要来显示具有单一意图的文本?基本上制作另一个 XML 首选项文件,当单击主首选项屏幕中的“关于”时将显示该文件?
    • 不不,每个首选项都可以包含标题和摘要。我说的是在该摘要中添加您的内容。无论如何,在更经典的布局中写下关于页面不是更好吗?例如,LinearLayout。因为PreferenceScreen 最适合显示不同主题的列表或链接列表(在我看来,ListView 更适合)。为了定制,我会选择另一件事,而不是 PreferenceScreen 持有您的关于文本。
    【解决方案3】:

    我遇到了同样的问题,需要显示一个静态文本块。尽管在我的情况下,它已排入首选项页面。

    标签确实允许链接,尽管这并不能解决对静态文本的需求。但是,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."/>
    

    【讨论】:

    • 注意:Preference 标签还有一个名为 android:fragment 的属性,它似乎链接到一个偏好片段。结合以上内容,您似乎需要“关于”框。
    【解决方案4】:

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

    (灵感来自: 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)
    

    【讨论】:

      【解决方案5】:

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

      我按照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);
          }
      

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

      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>
      

      【讨论】:

        猜你喜欢
        • 2012-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多