2017-02-26 2 views
0

ナビゲーション・ドロワーにヘッダーを追加しようとしています。 私はアンドロイドスタジオの初心者ですから、どんなヒントも私を助けてくれるでしょうナビゲーションドロワーのヘッダーがコンテンツの上にありますか?

問題は、このヘッダーがナビゲーションドロワーの上にあることです。私は自分のナビゲーションドロワー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のヘッダータグ内のヘッダーを作成します。あなたのlayout/nav_header.xml

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

を:コンテンツが使用ヘッダーのレイアウトにアクセスするには、ヘッダの内容

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

thaあなたは友よ!だから私のtextviewはnav_header.xmlにもありますか?それを行うにはどうすればヘッダー内のテキストビューにテキストを追加できますか?このxmlの 'setContentView(R.layout.base_activity);とこのテキストビューにテキストを追加するための' nh_User.setText(Username); 'がありますが、nav_header.xmlを作成するので、' nh_User'をもう参照できません... –

+1

回答を編集しました@RickJoe – rafsanahmad007

+0

ありがとうございます! –

関連する問題