How to create settings screen in android app?

廉价感情. 提交于 2019-12-13 09:22:24

问题


How do I create a settings screen? Should I use PreferenceActivity or PreferenceFragment?

Note- Documentation wasn't helpful

What I've already tried:

  1. Using PreferenceActivity but couldn't implement a simple toolbar in it.
  2. Using PreferenceFragment but was too complex to understand.

What I want is a simple implementation using PreferenceFragment


回答1:


On Android 3.0 and later, you should use a PreferenceFragment that displays your app settings.

Create a 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);
   }
   ...
}

And show it in your activity, like any other fragment:

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();
   }
}

You can include a toolbar in your activity xml, and add a container for SettingsFragment under it, this way the toolBar and your SettingsFragment will both be shown)

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

If you don't use androidx (but you'd better use), you can use toolbar in your xml like:

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

Notice: you need to use an attribute android:layout_marginTop="?android:attr/actionBarSize" (or if you're using the AppCompat library: ?attr/actionBarSize) for your fragment container will appear under toolbar

Within your activity don't forget to override onClick() method for your toolbar:

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



回答2:


Here is a complete working code for implementing settings in android:

add this library to your project

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

  • Create SettingsActivity and add it to 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>
  • Run SettingsActivity and test


来源:https://stackoverflow.com/questions/55314972/how-to-create-settings-screen-in-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!