2017-02-26 26 views
0

我想在我的抽屜式導航欄中添加一個頭。 我是一個Android工作室的初學者,所以任何提示將幫助我很多導航抽屜標題已結束內容?

問題是這個標題是在我的導航抽屜itens,所以我不能看到我的一些導航抽屜itens。

我怎樣才能正確地添加這個頭?另外,我的textview內容沒有顯示,爲什麼?

<?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" 
    tools:context="com.sk.mf.UserAreaActivity" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#fff" 
    android:id="@+id/drawerLayout"> 


    <FrameLayout 
     android:layout_height="wrap_content" android:layout_width="wrap_content" 
     android:id="@+id/content_frame"> 
    </FrameLayout> 


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



     <RelativeLayout 
      android:orientation="vertical" android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:background="@color/colorPrimary"> 

      <TextView 
       android:text="TextView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_centerHorizontal="true" 
       android:id="@+id/nh_User" 
       android:textSize="24sp" 
       android:textColor="#ffffff" /> 

     </RelativeLayout> 






    </android.support.design.widget.NavigationView> 



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

回答

1

創建的NavigationView頭標記頭:

<android.support.design.widget.NavigationView 
    android:id="@+id/navigation_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="@color/white" 
    app:headerLayout="@layout/nav_header" //layout of Header 
    app:itemIconTint="@color/colorPrimary" 
    app:menu="@menu/menu_navigation" /> 
layout/nav_header.xml

:添加頁眉內容

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="190dp" 
    android:orientation="vertical"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/transparent" 
     android:scaleType="fitXY" 
     android:src="@drawable/drawer_header" /> 
</LinearLayout> 

要訪問的頭佈局內容使用:

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view); 
View header = navigationView.getHeaderView(0); //get the header view 
TextView text = (TextView) header.findViewById(R.id.nh_User); 
text.setText("Hello"); 
+0

塔你的朋友!所以我的textview也會在nav_header.xml中呢?這樣做,我該如何在標題內的文本視圖中添加文本?因爲我有'的setContentView(R.layout.base_activity);'這個XML和'nh_User.setText(用戶名);'添加一些文字在這個TextView的,而是建立一個nav_header.xml我不能引用'nh_User'了...... –

+1

我已經編輯了答案@RickJoe – rafsanahmad007

+0

非常感謝你! –