【问题标题】:How to add preference XML inside a layout in a fragment如何在片段的布局内添加首选项 XML
【发布时间】:2018-08-28 03:56:34
【问题描述】:

我想实现查看配置文件活动以显示用户的姓名和图像,下面是首选项屏幕。我在 android studio 中使用了 PreferenceFragmentCompat,但它根本不起作用。我想将我的视图小部件与首选项屏幕结合起来。

这是我的设置片段:

public  class FragmentSettings extends PreferenceFragmentCompat{

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    addPreferencesFromResource(R.xml.preference_settings);
}


@Override
public View onCreateView(LayoutInflater layoutInflater , @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
    return layoutInflater.inflate(R.layout.fragment_settings,container,false);
}

}

这是我的preference_settings.xml

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

<android.support.v7.preference.PreferenceCategory
    android:title="Category 1">

    <android.support.v7.preference.SwitchPreferenceCompat
        android:key="key1"
        android:title="Switch Preference"
        android:summary="Switch Summary"
        android:defaultValue="true" />

    <android.support.v7.preference.EditTextPreference
        android:key="key2"
        android:title="EditText Preference"
        android:summary="EditText Summary"
        android:dialogMessage="Dialog Message"
        android:defaultValue="Default value" />
    <android.support.v7.preference.CheckBoxPreference
        android:key="key3"
        android:title="CheckBox Preference"
        android:summary="CheckBox Summary"
        android:defaultValue="true"/>
</android.support.v7.preference.PreferenceCategory>

这里是 fragment_settings.xml 视图:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:src="@mipmap/user" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageView"
        android:textSize="25dp"
        android:textAlignment="center"
        android:text="TextView" />

    <ListView
        android:id="@+id/android:list"
        android:layout_below="@id/textView4"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

我想实现这样的外观:

【问题讨论】:

    标签: android preferences preferencefragment


    【解决方案1】:

    如果您想将FragmentSettings 放入您的布局中,您可以在fragment_settings.xml 中使用片段容器并将您的FragmentSettings 放入其中。

    【讨论】:

      猜你喜欢
      • 2012-12-04
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多