【问题标题】:How to set a custom string in Preferences screen?如何在首选项屏幕中设置自定义字符串?
【发布时间】:2015-07-26 05:14:22
【问题描述】:

在我的preferences.xml 我有这个选项:

<PreferenceCategory android:title="@string/pref_header_storage">

        <Preference
            android:key="downloadLocation"
            android:title="@string/pref_title_wsl"
            android:summary="@string/pref_summary_wsl"
            android:persistent="true" />

    </PreferenceCategory>

我想知道是否可以为该偏好添加自定义文本,以便向用户显示该偏好的当前值。

所以最后的偏好可能是这样的:

下载文件夹

选择一个文件夹来保存您的下载。

当前选择的文件夹:XXXXXXX

我想在该首选项中添加最后一行,但我不知道该怎么做。

有人可以帮助我吗?提前致谢。

【问题讨论】:

  • 您希望文本显示为摘要还是希望它们位于首选项下方?
  • @Renges 在首选项下方。不作为总结。

标签: android download sharedpreferences settings preferences


【解决方案1】:

将您的首选项放在 PreferenceFragment 中。然后使用这个将其加载到您的设置活动中

getFragmentManager().beginTransaction().replace(R.id.container, new PreferencesFragment()).commit();

您的偏好片段应该扩展 PreferenceFragment

public class PreferencesFragment extends PreferenceFragment

这样,您可以编辑 Preference Activity 的 xml 并将文本添加到 Preference Fragment 的容器下方。

设置活动的示例代码:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center" >

    <include layout="@layout/toolbar"/>

    <FrameLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:orientation="vertical" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".SettingActivity" tools:ignore="MergeRootFrame"
        android:layout_below="@+id/toolbar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:foreground="?android:windowContentOverlay">
    </FrameLayout>

    <TextView
        android:id="@+id/text_below_prefernces"
        android:layout_below="@+id/container"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

    </TextView>

</RelativeLayout>

【讨论】:

  • 我已经有了片段的代码,可以展示它,更改首选项值的代码,一切正常。我只想在首选项中添加该文本(只是在该首选项中(还有其他)),但我不知道如何。
  • 我已经为 Activity 添加了布局代码。你能检查一下是不是你想要的吗?
  • 它在片段下方添加文本。我希望它在偏好内容中。
  • 感谢您的宝贵时间
猜你喜欢
  • 2019-06-03
  • 1970-01-01
  • 2016-05-16
  • 1970-01-01
  • 1970-01-01
  • 2015-07-13
  • 2017-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多