Android 操作栏行为异常
Android actionbar behaving strangely
我正在尝试创建一个美观的 SearchView。我使用 this 代码并将其集成到我的 DrawerLayout 中。这是我目前的XML主activity:`
<?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/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/app_bar_main"/>
<include layout="@layout/app_bar_main_search"
android:visibility="gone"/>
</RelativeLayout>
<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"
android:visibility="gone"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
这是我的 app_bar_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
app:titleTextColor="@color/white"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/AppTheme.AppBarOverlay"/>
我的app_bar_main_search.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar android:id="@+id/searchtoolbar"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
app:collapseIcon="@drawable/ic_arrow_left"
app:titleTextColor="@color/colorPrimary"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/white"/>
我的 main_layout.xml 只是一个带有 FAB 和空约束布局的文件。
上面的代码有效,但主要布局在操作栏下方,左上角的 NavigationDrawer 图标停止工作(除非我通过滑动将 NavigationDrawer 取出,然后由于某种原因它再次开始工作) .移除操作栏周围的 RelativeLayout 会使 app_bar_main 覆盖整个屏幕。为 app_bar_main 分配一个 ID 使其完全为空,只是一种背景颜色。
我做错了什么?如何在操作栏下方获取主布局?
编辑:我的工具栏与@FAT 的答案一起工作,但导航抽屉图标仍然无法正常工作。这是相关代码:
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(0).setChecked(true);
我尝试将 syncState() 添加到 onPostCreate,但这并没有改变任何东西。当我单击该图标时,onOptionsItemSelected 函数也不会被调用。
DrawerLayout
最多应该有两个视图。在 DrawerLayout
中,添加一个包含屏幕 main
内容的视图(隐藏抽屉时的主要布局)和另一个包含 navigation
抽屉内容的视图。
To use a DrawerLayout
, position your primary content
view as the
first
child with width and height of match_parent and no
layout_gravity>. Add drawers
as child views after the main content
view and set the layout_gravity appropriately. Drawers commonly use
match_parent for height with a fixed width.
1. 将您的 main_layout
放在第一个直接子 RelativeLayout
.
中
2. 添加属性 android:layout_below="@id/app_bar_main"
到 main_layout.
更新您的布局 XML 如下:
<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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/appbar"
layout="@layout/app_bar_main"/>
<include layout="@layout/app_bar_main_search"
android:visibility="gone"/>
<include
layout="@layout/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/appbar"/>
</RelativeLayout>
<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"
android:visibility="gone"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
希望对你有所帮助~
我正在尝试创建一个美观的 SearchView。我使用 this 代码并将其集成到我的 DrawerLayout 中。这是我目前的XML主activity:`
<?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/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/app_bar_main"/>
<include layout="@layout/app_bar_main_search"
android:visibility="gone"/>
</RelativeLayout>
<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"
android:visibility="gone"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
这是我的 app_bar_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
app:titleTextColor="@color/white"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/AppTheme.AppBarOverlay"/>
我的app_bar_main_search.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar android:id="@+id/searchtoolbar"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
app:collapseIcon="@drawable/ic_arrow_left"
app:titleTextColor="@color/colorPrimary"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/white"/>
我的 main_layout.xml 只是一个带有 FAB 和空约束布局的文件。
上面的代码有效,但主要布局在操作栏下方,左上角的 NavigationDrawer 图标停止工作(除非我通过滑动将 NavigationDrawer 取出,然后由于某种原因它再次开始工作) .移除操作栏周围的 RelativeLayout 会使 app_bar_main 覆盖整个屏幕。为 app_bar_main 分配一个 ID 使其完全为空,只是一种背景颜色。
我做错了什么?如何在操作栏下方获取主布局?
编辑:我的工具栏与@FAT 的答案一起工作,但导航抽屉图标仍然无法正常工作。这是相关代码:
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(0).setChecked(true);
我尝试将 syncState() 添加到 onPostCreate,但这并没有改变任何东西。当我单击该图标时,onOptionsItemSelected 函数也不会被调用。
DrawerLayout
最多应该有两个视图。在 DrawerLayout
中,添加一个包含屏幕 main
内容的视图(隐藏抽屉时的主要布局)和另一个包含 navigation
抽屉内容的视图。
To use a
DrawerLayout
, position your primarycontent
view as thefirst
child with width and height of match_parent and no layout_gravity>. Adddrawers
as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.
1. 将您的 main_layout
放在第一个直接子 RelativeLayout
.
2. 添加属性 android:layout_below="@id/app_bar_main"
到 main_layout.
更新您的布局 XML 如下:
<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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/appbar"
layout="@layout/app_bar_main"/>
<include layout="@layout/app_bar_main_search"
android:visibility="gone"/>
<include
layout="@layout/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/appbar"/>
</RelativeLayout>
<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"
android:visibility="gone"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
希望对你有所帮助~