Android 首选项重叠工具栏

标签 android android-actionbar android-toolbar android-preferences

我有一个偏好的安卓应用 enter image description here

如您所见,首选项与工具栏重叠。 这是我的主要 fragment xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

其中包含一个新的 xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include
        layout="@layout/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_partial_secure" />

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

最后是 content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.gilca.ebspma.MainActivity">

    <TextView
        android:id="@+id/location"
        android:paddingTop="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

我的 xml 偏好是这个

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <PreferenceCategory
        android:summary="Username and password information"
        android:title="Informações de Login">
        <EditTextPreference
            android:key="username"
            android:summary="Introduzir username"
            android:title="Username" />
        <EditTextPreference
            android:key="password"
            android:summary="Introduzir password"
            android:title="Password" />
    </PreferenceCategory>
    <PreferenceCategory
        android:summary="sessao"
        android:title="Definições">
        <Preference
            android:key="@string/myCoolButton"
            android:summary="Mude aqui"
            android:title="Mudar a password na BD" />
        <CheckBoxPreference
            android:key="checkBox"
            android:summary="On/Off"
            android:title="Manter Sessão" />
    </PreferenceCategory>
</PreferenceScreen>

而这个xml是从SettingsFragment.java调用的

public class SettingsFragment extends PreferenceFragmentCompat {
    SharedPreferences sharedPreferences;

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        addPreferencesFromResource(R.xml.prefs);
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    }
}

那么......为什么我会遇到这个问题,我该如何解决?

更新 代码询问

 public boolean onNavigationItemSelected(MenuItem item) {
    Fragment fragment = null;
    Class fragmentClass = null;
    int id = item.getItemId();
    if (id == R.id.nav_home) {
        fragmentClass = HomeFragment.class;
    } else if (id == R.id.nav_req) {


    } else if (id == R.id.nav_ver) {

    } else if (id == R.id.nav_atividades) {
        fragmentClass = AtividadesFragment.class;
    } else if (id == R.id.nav_training) {
        fragmentClass = TrainingFragment.class;
    } else if (id == R.id.nav_settings) {
        fragmentClass = SettingsFragment.class;
    }
    else if (id == R.id.nav_about) {
    }
    else if (id == R.id.nav_sair) {
        session.logOut();
        System.exit(0);
    }
    try{
        assert fragmentClass != null;
        fragment = (Fragment) fragmentClass.newInstance();
    }catch (Exception e) {
        e.printStackTrace();
    }

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content, fragment).commit();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

最佳答案

尝试将 main.xml 中的“内容”框架布局对齐到“app_bar”下方。

<include
android:id="@+id/app_bar"
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />


<FrameLayout
android:id="@+id/content"
android:layout_below="@id/app_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

关于Android 首选项重叠工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35280441/

相关文章:

android - 如何仅从 Android Studio 中的特定对话中获取已发送的消息?

android - 即使使用 setHomeButtonEnabled(true) 应用程序图标在 Android 14 中也不可点击

Android:设置工具栏主题会导致应用崩溃

android - 错误 :(3, 17) java : package org. junit 不存在-intellij

java - Android Studio 1.5.1 重复文件复制到 APK META-INF/NOTICE.txt commons-codec-1.9.jar ...?

android - 我应该始终对所有内容使用 ConstraintLayout 吗?

搜索小部件的 Android 自定义布局

android - 不同的操作栏适合不同的 Activity

Android 设计支持 TabLayout 重叠内容

Android Lollipop,从工具栏中的标题添加弹出菜单