如何在Android应用中创建设置屏幕? [英] How to create settings screen in android app?

查看:336
本文介绍了如何在Android应用中创建设置屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

注意-文档没有帮助

我已经尝试过的:

  1. 使用 PreferenceActivity ,但无法在其中实现一个简单的工具栏.
  2. 使用 PreferenceFragment ,但是太复杂了,难以理解.
  1. Using PreferenceActivity but couldn't implement a simple toolbar in it.
  2. Using PreferenceFragment but was too complex to understand.

我想要的是一个使用 PreferenceFragment

推荐答案

在Android 3.0及更高版本上,您应该使用显示您的应用设置的PreferenceFragment.

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

创建一个SettingsFragment:

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

您可以在活动xml中包含一个工具栏,并在其下面添加一个用于SettingsFragment的容器,这样,将同时显示toolBar和您的SettingsFragment)

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>

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

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

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

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

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

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

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

这篇关于如何在Android应用中创建设置屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆