如您所见,首选项与工具栏重叠。 这是我的主要片段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());
}
}
所以...为什么我有这个问题,我该如何解决?
UPDATE 代码问了
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;
}
答案 0 :(得分:0)
尝试将main.xml中的“内容”框架布局与“app_bar”对齐。
setVisible(true)
答案 1 :(得分:0)
我对你的布局层次有点困惑,特别是关于
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
和
<include
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar" />
最终可以用于同一个函数,它是主要的片段容器。
但是,我知道这个问题可能是一个真正的屁股,我最近一直在努力(实际上很多)并最终找到答案。至少在我的情况下,它只是通过添加LinearLayout
作为AppBarLayout
和您的片段容器的父项来解决(在这种情况下,我不确切地知道哪个是FrameLayout
,{ {1}}或您的content_main
布局。
如果我的解释不够清楚,请参阅Fragment overlaps my AppCompat toolbar。
答案 2 :(得分:0)
与其做如此复杂的事情,只需容纳一个 AppBar 和 FloatingActionButton,您可以执行以下操作:
在设置活动的布局文件中(例如,activity_settings.xml
)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".SettingsActivity"
android:orientation="vertical"
android:nestedScrollingEnabled="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:liftOnScroll="true">
<androidx.appcompat.widget.Toolbar
...
/>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container"/>
<FloatingActionButton
...
/>
</LinearLayout>
任何常规的偏好片段都可以。