在布局中插入偏好设置XML

3
我知道我的问题有点奇怪,但这是故事背景。我在我的应用程序中添加了一个PreferenceFragment,我正在用SettingsFragment替换主布局中的一个碎片,但我遇到了一个问题:XML内容显示为match_parent,并且显示在工具栏上方,而我希望它显示在工具栏下方。
为什么会发生这种情况?因为碎片布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.abohani.ramadantime.MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways|snap"
    android:minHeight="@dimen/abc_action_bar_default_height_material"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


<FrameLayout
    android:id="@+id/fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    />

</RelativeLayout>

我无法更改该片段并将其设置在工具栏下面,否则我会错过我的应用程序的大部分内容(我添加了它的原因)。
所以,简而言之,我的问题是内容重叠了工具栏,我已尝试以下方法:
    <Preference
    android:layout="@layout/space"
    />

空间布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
    android:id="@+id/views"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:visibility="invisible"
    />

</RelativeLayout>

它起作用了,但是滚动时不起作用,内容会在工具栏上方滚动。
所以,我需要类似于“below:@id/layout”的东西。
有什么想法吗?

除了我下面的回答之外,尽可能使用LinearLayout,因为您不会利用RelativeLayout的任何功能,那么为什么要增加开销呢? :P - apelsoczi
1个回答

3

Android开发者网站上使用的模式已经过时至API 10时代。以下是一个示例,演示如何使用现代的Activity/Fragment设计模式实现首选项 - 如果您正在平板电脑上使用主/细节流程,则也可以使用此方法。

pref_general.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ListPreference
        android:icon="@drawable/ic_sort_black_48dp"
        android:title="@string/pref_sort_label"
        android:key="@string/pref_sort_key"
        android:defaultValue="@string/pref_sort_favorite"
        android:entryValues="@array/pref_sort_values"
        android:entries="@array/pref_sort_options" />

</PreferenceScreen>

arrays.xml

<resources>

    <string-array name="pref_sort_options">
        <item>@string/pref_sort_label_popular</item>
        <item>@string/pref_sort_label_rating</item>
        <item>@string/pref_sort_label_favorite</item>
    </string-array>

    <string-array name="pref_sort_values">
        <item>@string/pref_sort_popular</item>
        <item>@string/pref_sort_rating</item>
        <item>@string/pref_sort_favorite</item>
    </string-array>

</resources>

strings.xml

<string name="pref_sort_label">Sort Order</string>
<string name="pref_sort_label_popular">Most Popular</string>
<string name="pref_sort_label_rating">Top Rated</string>
<string name="pref_sort_label_favorite">Favorites</string>
<string name="pref_sort_key" translatable="false">sort</string>
<string name="pref_sort_popular" translatable="false">popular</string>
<string name="pref_sort_rating" translatable="false">rating</string>
<string name="pref_sort_favorite" translatable="false">favorites</string>

activity_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SettingsActivity"
    tools:ignore="MergeRootFrame">

    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />

    <FrameLayout
        android:id="@+id/container_settings"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:theme="@style/ToolbarTheme"
    android:elevation="4dp"
    app:titleMarginStart="32dp"
    app:popupTheme="@style/PopupTheme">

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

SettingsActivity.java

import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        getFragmentManager().beginTransaction()
                .replace(R.id.container_settings, new SettingsFragment())
                .commit();
    }

    public static class SettingsFragment extends PreferenceFragment
            implements Preference.OnPreferenceChangeListener {

        public static String TAG = SettingsFragment.class.getSimpleName();

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.pref_general);
            bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_sort_key)));
        }

        private void bindPreferenceSummaryToValue(Preference preference) {
            preference.setOnPreferenceChangeListener(this);

            onPreferenceChange(preference,
                    PreferenceManager
                            .getDefaultSharedPreferences(preference.getContext())
                            .getString(preference.getKey(), ""));
        }

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            String stringValue = newValue.toString();

            if (preference instanceof ListPreference) {
                ListPreference listPreference = (ListPreference) preference;
                int prefIndex = listPreference.findIndexOfValue(stringValue);
                if (prefIndex >= 0) {
                    preference.setSummary(listPreference.getEntries()[prefIndex]);
                }
            }
            else {
                preference.setSummary(stringValue);
            }

            return true;
        }
    }
}

我之前在使用这个,但是现在必须使用一个Fragment,所以这个方法行不通了。 - Jaeger
你自己决定!以上是一种行之有效的方法。你正在使用一个新的Activity吗?还是使用了应用程序中其余部分所在的Activity? - apelsoczi
我改用手动设置布局的顶部边距来解决PreferenceFragment中的问题,这很困难和烦人,更不用说浪费的时间了,但它现在可以工作了,感谢你的帮助 :)。 - Jaeger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接