如何在android应用程序中创建设置屏幕?
如何创建设置屏幕? 我应该使用PreferenceActivity还是PreferenceFragment 注意-文档没有帮助 我已经尝试过的:如何在android应用程序中创建设置屏幕?,android,preferenceactivity,preferencefragment,Android,Preferenceactivity,Preferencefragment,如何创建设置屏幕? 我应该使用PreferenceActivity还是PreferenceFragment 注意-文档没有帮助 我已经尝试过的: 正在使用PreferenceActivity,但无法在其中实现简单的工具栏 使用PreferenceFragment,但太复杂而无法理解 我想要的是在Android 3.0及更高版本上使用PreferenceFragment的简单实现,您应该使用PreferenceFragment来显示应用程序设置 创建设置片段: public static clas
我想要的是在Android 3.0及更高版本上使用PreferenceFragment的简单实现,您应该使用PreferenceFragment来显示应用程序设置 创建设置片段:
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
...
}
并将其显示在活动中,就像其他片段一样:
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
您可以在活动xml中包含工具栏,并在其下添加用于设置片段的容器,这样工具栏和设置片段都将显示出来)
以下是在android中实现设置的完整工作代码: 将此库添加到项目中
implementation'androidx.首选项:首选项:1.1.0-alpha02'
- 创建设置活动并将其添加到清单
<activity android:name=".SettingsActivity"/>
设置片段public class SettingsFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String s) { addPreferencesFromResource(R.xml.preferences); // implement your settings here } }
<activity android:name=".SettingsActivity"/>
preferences.xmlpublic class SettingsFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String s) { addPreferencesFromResource(R.xml.preferences); // implement your settings here } }
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.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"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/color_primary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <TextView android:id="@+id/toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Settings" android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" android:textColor="@android:color/white" android:textSize="16sp" /> </androidx.appcompat.widget.Toolbar> </com.google.android.material.appbar.AppBarLayout> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?> <androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <androidx.preference.PreferenceCategory android:title="General Settings"> <androidx.preference.Preference android:key="general_settings_key" android:title="Preference Test" /> <androidx.preference.SwitchPreferenceCompat android:defaultValue="true" android:key="switch_settings_key" android:title="Switch" /> </androidx.preference.PreferenceCategory> <androidx.preference.PreferenceCategory android:title="Other"> <androidx.preference.Preference android:key="about_settings_key" android:title="About Me" /> </androidx.preference.PreferenceCategory> </androidx.preference.PreferenceScreen>
- 运行设置活动和测试
,在setContentView()
setContentView()行显示错误。错误是错误膨胀类androidx.appcompat.widget.toolBar尝试将此依赖项添加到gradle文件:实现“androidx.appcompat:appcompat:1.1.0-alpha03”实现“androidx.constraintlayout:constraintlayout:1.1.3我在项目中使用androidx库”),或者您可以使用如下工具栏:最后,我会在我的回答中添加代码!工具栏是可见的,但它是重叠的首选项。尝试了
,如您所说,但没有很好地工作!现在你可以把这个答案标记为正确答案!android:layout_marginTop=“?attr/actionBarSize”
public class SettingsFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String s) { addPreferencesFromResource(R.xml.preferences); // implement your settings here } }
<?xml version="1.0" encoding="utf-8"?> <androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <androidx.preference.PreferenceCategory android:title="General Settings"> <androidx.preference.Preference android:key="general_settings_key" android:title="Preference Test" /> <androidx.preference.SwitchPreferenceCompat android:defaultValue="true" android:key="switch_settings_key" android:title="Switch" /> </androidx.preference.PreferenceCategory> <androidx.preference.PreferenceCategory android:title="Other"> <androidx.preference.Preference android:key="about_settings_key" android:title="About Me" /> </androidx.preference.PreferenceCategory> </androidx.preference.PreferenceScreen>