如何在Android应用程序中创建设置屏幕?

问题描述 投票:-2回答:2

如何创建设置屏幕?我应该使用PreferenceActivity还是PreferenceFragment?

注意 - 文档没有帮助

我已经尝试过的:

  1. 使用PreferenceActivity但无法在其中实现简单的工具栏。
  2. 使用PreferenceFragment但是太复杂而无法理解。

我想要的是使用PreferenceFragment的简单实现

android preferenceactivity preferencefragment
2个回答
2
投票

在Android 3.0及更高版本中,您应该使用显示应用程序设置的PreferenceFragment。

创建一个SettingsFragment:

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中包含一个工具栏,并在其下添加一个容器SettingsFragment,这样就可以显示toolBar和SettingsFragment)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_catalog_root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="-20dp">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/fragment_catalog_first_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00000000"
        app:elevation="0dp"
        >

        <androidx.appcompat.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/fragment_catalog_first_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/transparent_background"
            android:elevation="4dp"
            >

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>

    <FrameLayout
        android:id="@+id/activity_catalog_fragment_cont"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:attr/actionBarSize"
        android:animateLayoutChanges="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</RelativeLayout>

如果你不使用androidx(但你最好使用),你可以在你的xml中使用工具栏,如:

<android.support.v7.widget.Toolbar.....>

注意:您需要使用属性android:layout_marginTop="?android:attr/actionBarSize"(或者如果您使用的是AppCompat库:?attr/actionBarSize),您的片段容器将显示在工具栏下

在您的活动中,不要忘记覆盖工具栏的onClick()方法:

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       getActivity().onBackPressed();
    }
});

4
投票

这是一个用于在android中实现设置的完整工作代码:

将此库添加到您的项目中

implementation 'androidx.preference:preference:1.1.0-alpha02'

  • 创建SettingsActivity并将其添加到Manifest <activity android:name=".SettingsActivity"/>

activity_preferences.xml

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

SettingsActivity

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preferences);
        Toolbar toolbar = findViewById(R.id.toolbar);
        try {
            setSupportActionBar(toolbar);
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayShowTitleEnabled(false);
                actionBar.setDisplayHomeAsUpEnabled(true);
                actionBar.setDisplayShowHomeEnabled(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.container, new SettingsFragment()).commit();
    }
}

SettingsFragment

public class SettingsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String s) {
        addPreferencesFromResource(R.xml.preferences);
        // implement your settings here
    }
}

的preferences.xml

<?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>
  • 运行SettingsActivity并测试
© www.soinside.com 2019 - 2024. All rights reserved.