2017-05-30 3 views
0

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.

+0

helfen Wird es, wenn Sie die erste RelativeLayout über die erste enthalten setzen nicht? – cristianorbs

+0

@cristianorbs Nein, das macht die gesamte Aktionsleiste verschwinden, wenn der Bildschirm mit der Datei main_layout.xml gefüllt wird. – user2988879

+0

Und was, wenn Sie in Ihrer main_layout-Datei layout_marginTop = "? Attr/actionBarSize" in der übergeordneten Ansicht platzieren? – cristianorbs

Antwort

1

DrawerLayout sollte höchstens zwei Ansichten haben. Fügen Sie innerhalb der DrawerLayout eine Ansicht hinzu, die den Inhalt main für den Bildschirm enthält (Ihr primäres Layout, wenn die Schublade ausgeblendet ist) und eine weitere Ansicht, die den Inhalt der Schublade navigation enthält.

See documentation

> An ein DrawerLayout, positionieren Sie die primäre content Ansicht als first Kind mit einer Breite und Höhe von match_parent und ohne layout_gravity zu verwenden. Fügen Sie drawers als untergeordnete Ansichten nach dem Hauptinhalt hinzu und richten Sie die layout_gravity entsprechend ein. Schubladen verwenden häufig match_parent für Höhe mit einer festen Breite.

1. Halten Sie Ihre main_layout innerhalb der ersten direkten Kind RelativeLayout.

2. Fügen Sie das Attribut android:layout_below="@id/app_bar_main" zu main_layout hinzu.

XML-Update Ihr Layout wie folgt:

<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> 

Hope this ~

+0

Danke für deinen Kommentar, aber wenn ich der app_bar_main eine ID zuweise, verschwindet der Inhalt plötzlich. Es verwandelt sich einfach in eine leere Aktionsleiste. – user2988879

+0

Ich habe es funktioniert, das Problem war, dass die Symbolleiste innerhalb von app_bar_main bereits eine ID hatte, also habe ich diese entfernt und es dem Include hinzugefügt. Es funktioniert jetzt. Das NavigationDrawer-Symbol funktioniert jedoch immer noch nicht. – user2988879

+0

Ich verstehe. Für Schubladensymbol versuchen Sie dies >>> DrawerLayout mDrawerLayout = (DrawerLayout) findViewById (R.id.drawer_layout); ActionBarDrawerToggle mActionBarDrawerToggle = neues ActionBarDrawerToggle (dies, mDrawerLayout, mToolBar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); mDrawerLayout.setDrawerListener (mActionBarDrawerToggle); mActionBarDrawerToggle.syncState(); – FAT