簡體   English   中英

在布局中插入首選項XML

[英]Insert Preference XML inside a Layout

我知道我的問題有點奇怪,但這是故事。 我在我的應用程序中添加了一個PreferenceFragment,我正在用SettingsFragment替換主布局中的一個片段,但我遇到了問題,XML內容顯示在match_parent中,並顯示在工具欄上方,我希望它在下面工具欄。

為什么會這樣? 因為Fragment布局如下:

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

它工作,但滾動時,它不起作用,內容滾動工具欄上方。

所以,我需要一些接近“下面的內容:@id / layout”。

有任何想法嗎 ?

Android Developer網站上使用的模式可以追溯到API 10的時代。下面是一個示例,如何使用現代的Activity / Fragment設計模式實現Preferences - 如果您在平板電腦上使用主/詳細信息流,這也適用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM