2017-02-22 34 views
1

我有一個Activity的佈局,我試圖添加一個導航抽屜。DrawerLayout中視圖的大小和點擊行爲錯誤

的問題是,能正常工作,我需要使用:的

<android.support.v4.widget.DrawerLayout 

代替:

<RelativeLayout 

但它攪亂的事情了。我ProgressBar變得更大,我的RecyclerView不起作用,應用程序日誌我出去當我點擊的東西,等

我的佈局XML:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="com.st.mf.UserAreaActivity" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:padding="@dimen/activity_vertical_margin" 
    android:background="#fff"> 

    <ProgressBar 
     android:id="@+id/progressBar1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentLeft="true" 
     android:layout_marginTop="20dp" /> 

    <android.support.design.widget.NavigationView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     app:menu="@layout/navigation_menu" 
     android:layout_gravity="start"> 
    </android.support.design.widget.NavigationView> 

</android.support.v4.widget.DrawerLayout> 

我如何創建我的抽屜菜單不搞亂一切其他呢?

+1

'DrawerLayout'應該只有一個內容'View'。將'ProgressBar'和'RecyclerView'放在'ViewGroup'中,就像垂直的'LinearLayout'一樣。 –

+0

@MikeM。非常感謝你。你介意給我一個例子,讓我更好地想象它嗎? –

+0

你的意思是一個佈局XML的例子? –

回答

1

任何直接孩子DrawerLayout這不是一個抽屜被認爲是一個內容View,並且在兩個方向將被佈置到match_parent,無論寬度和高度屬性你已經在其上設置的View。在你的情況下 - 實際上,在大多數情況下 - 你只需要一個內容View,所以非抽屜的其餘部分都應該在一個ViewGroup之內。

我們會將您的ProgressBarRecyclerView放在RelativeLayout的內部,該內容充當內容View,他們將在其中保留您設置的佈局屬性。例如:

<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" 
    tools:context="com.st.mf.UserAreaActivity" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="@dimen/activity_vertical_margin" 
    android:background="#fff"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ProgressBar 
      android:id="@+id/progressBar1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recyclerView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentStart="true" 
      android:layout_alignParentLeft="true" 
      android:layout_marginTop="20dp" /> 

    </RelativeLayout> 

    <android.support.design.widget.NavigationView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     app:menu="@layout/navigation_menu" 
     android:layout_gravity="start"> 
    </android.support.design.widget.NavigationView> 

</android.support.v4.widget.DrawerLayout> 

注意,內容View應始終任何抽屜前上市,以保持適當的Z排序;即將抽屜保持在內容之上。