Ich versuche, ein gut aussehendes SearchView zu erstellen. Ich benutzte this Code und integrierte es in meinem DrawerLayout. Dies ist der aktuelle XML meiner Haupttätigkeit: `Android actionbar verhält sich seltsamerweise
<?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>
Das ist mein 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"/>
Mein 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"/>
Mein main_layout.xml ist nur eine Datei mit einem FAB und einem leeren Constraintlayout.
Der obige Code funktioniert, aber das Hauptlayout befindet sich unterhalb der Aktionsleiste und das NavigationDrawer-Symbol in der oberen linken Ecke funktioniert nicht mehr (außer wenn ich den NavigationDrawer durch Wischen entferne, dann fängt er wieder an zu arbeiten). Wenn Sie RelativeLayout um die Aktionsleisten entfernen, wird der gesamte Bildschirm von app_bar_main abgedeckt. Wenn Sie der app_bar_main eine ID zuweisen, ist sie vollständig leer, nur eine Hintergrundfarbe.
Was mache ich falsch? Wie kann ich das Hauptlayout unterhalb der Aktionsleiste bekommen?
EDIT: Ich habe die Symbolleiste arbeiten mit @ FAT-Antwort, aber das Navigationstrigger-Symbol funktioniert immer noch nicht. Dies ist der entsprechende Code:
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);
ich die syncState versuchte das Hinzufügen() zu dem onPostCreate aber das hat nichts ändern. Die onOptionsItemSelected-Funktion wird auch nicht aufgerufen, wenn ich auf das Symbol klicke.
helfen Wird es, wenn Sie die erste RelativeLayout über die erste enthalten setzen nicht? – cristianorbs
@cristianorbs Nein, das macht die gesamte Aktionsleiste verschwinden, wenn der Bildschirm mit der Datei main_layout.xml gefüllt wird. – user2988879
Und was, wenn Sie in Ihrer main_layout-Datei layout_marginTop = "? Attr/actionBarSize" in der übergeordneten Ansicht platzieren? – cristianorbs