Android PreferenceScreen 位于工具栏之上。

4
以下是偏好设置屏幕的截图: 显示问题的截图 我还包括布局和Java代码:

layout/toolbar.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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary_dark"
android:elevation="0dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
tools:ignore="UnusedAttribute" />

layout/activity_settings.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/toolbar" />
</LinearLayout>

xml/fragment_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Notifications">
        <SwitchPreference
            android:defaultValue="false"
            android:key="currencyNotificationOnOff"
            android:title="@string/notification_enable" />

        <com.adwitiya.currencyplus.view.TimePickerDialog
            android:defaultValue="08:00"
            android:dependency="currencyNotificationOnOff"
            android:key="scheduleNotificationTime"
            android:showDefault="true"
            android:title="Notification Time" />
    </PreferenceCategory>
    <PreferenceCategory android:title="">
        <Preference
            android:key="about"
            android:title="@string/about" />
        <Preference
            android:key="disclaimer"
            android:title="@string/disclaimer" />
        <Preference
            android:key="help"
            android:title="@string/help" />
    </PreferenceCategory>
    <PreferenceCategory android:title="General">
        <Preference
            android:key="spreadWord"
            android:title="@string/prefs_spread_word" />
        <Preference
            android:key="rateUs"
            android:title="@string/rate_us" />
        <Preference
            android:key="feedback"
            android:title="@string/feedback" />
    </PreferenceCategory>
</PreferenceScreen>

SettingsActivity.java

public class SettingsActivity extends ActionBarActivity {

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

        setContentView(R.layout.activity_settings);

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

        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment()).commit();
        TimePickerDialog timePicker = new TimePickerDialog(this);
        PreferenceManager.setDefaultValues(this, R.xml.fragment_settings, false);
    }
}

SettingsFragment.java

public class SettingsFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.fragment_settings);

        handleSettingsOptions();
    }
    ....
}

我无法将工具栏保持在顶部并使首选项屏幕元素显示在其下方。当选项被滚动时,它会移动到工具栏的顶部。

我正在使用单个首选项屏幕,每个首选项都会打开一个 Webview。

感谢您提前的回复。


你解决了吗?我也遇到了同样的问题。 - Jakkra
很遗憾,我仍在处理这个问题。一旦我能够解决它,我会发布一些内容。 - Nagamohan
我发现的是PreferenceScreen的背景在工具栏后面,而PreferenceScreen的内容则在工具栏上方呈现!这真是令人困惑。目前还没有找到好的解决方案(除了下面的解决方法)。 - FrozenCow
3个回答

3

希望这对那些被接受的答案无法解决问题的人有所帮助。

Layout/activity_setting.xml中,将代码更改为以下内容:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<include layout="@layout/toolbar" />

<!-- added code -->
<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</LinearLayout>

在SettingsActivity.java中,替换此代码。
getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment()).commit();

通过这个

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

2

问题已经解决。我所做的更改如下:

  1. 从“activity_settings.xml”中删除了工具栏。
  2. 从“SettingsActivity.java”中删除了与工具栏相关的代码。
  3. 在“AndroidManifest.xml”中,我为该活动添加了“Theme.AppCompat.Light.DarkActionBar”主题(根据您的要求进行更改)。

在从Theme.AppCompat.Light.DarkActionBar更改为NoActionBar并添加适当的布局(CoordinatorLayout、AppBarLayout等)后,我遇到了相同的问题。我更改了主题,以便将首选项活动的主题与我的主要活动的主题匹配。这是“必需的”,因为默认工具栏的标题文本(来自DarkActionBar主题的工具栏)与大多数其他Material设计应用程序相比太小了。此外,工具栏需要在向下滚动时移开。我还没有找到解决这个问题的正确答案。 - FrozenCow
加入一个菜单选项来重置所有设置怎么样?没有工具栏你无法完成这个操作。 - VasileM

0
以下方法对我有效:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include layout="@layout/toolbar" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/contentPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

这样您仍然可以使用常规工具栏,其主题将保持完好无损。


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