【发布时间】:2017-05-30 18:42:04
【问题描述】:
我正在尝试创建一个好看的 SearchView。我使用了this 代码并将其集成到我的 DrawerLayout 中。这是我主要活动的当前 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/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 函数。
【问题讨论】:
-
如果你把第一个RelativeLayout放在第一个include上面会不行吗?
-
@cristianorbs 不,这会使整个操作栏消失,屏幕被 main_layout.xml 填满
-
如果在你的 main_layout 中,你把 layout_marginTop="?attr/actionBarSize" 放在父视图上呢?
-
@cristianorbs 是的,我也试过了,由于某种原因根本没有做任何事情
-
您是否已经尝试将相对布局高度更改为 wrap_content 而不是 match_parent?
标签: android xml android-actionbar searchview