Android PreferenceScreen 位于工具栏之上

标签 android android-layout

下面是首选项屏幕的截图:

screenshot showing problem

我还包括布局和 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 。

预先感谢您的回复。

最佳答案

希望这对其他接受的答案不适合你的人有用。

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

关于Android PreferenceScreen 位于工具栏之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28189848/

相关文章:

android - 在屏幕底部设置布局,其余部分填充父级

java - 在 Eclipse 中将线性布局中的文本居中对齐?

Android-以编程方式实现 'Show Touches'

java - 此 LoginActivity 在哪里检索电话号码。和密码来自?

java - android中不显示特殊字符

android - 我的 UI 在设计器中和在设备上看起来不同

android - 覆盖 Android 工具栏默认填充是否被认为是不好的做法?

java - 如何在android ImageView 中显示tomcat中本地服务器的图像

java - Android,第二个 Activity 未启动

android - 由于 Android 中的微调器,两个 Activity 之间的转换滞后