【发布时间】: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