2017-03-21 19 views
1

の後ろにドロワーの背景に画像を持たせたい。このコードでは、の画像がメニューの前にあります。私は確信していませんが、私はこの特別な場合にFrameLayoutを使用することはできません。NavigationDrawer with background画像

画像もそのアスペクト比とその前にとどまるべき半透明の色を維持しなければなりません。この色は、あなたが新しいImageViewのように背景を設定しないでください/ @色によってmenuSemi

<?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/content_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <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" 
     app:headerLayout="@layout/nav_header_main" 
     android:background="@color/menuSemi" 
     app:itemTextColor="@color/white" 
     app:itemIconTint="@color/white" 
     android:paddingTop="16dp" 
     style="@style/AppTheme.navigation" 
     app:menu="@menu/activity_main"> 

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

      <ImageView 
       android:id="@+id/drawer_bg" 
       android:src="@drawable/sidebar" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:alpha="1" 
       android:scaleType="centerCrop"/> 
     </RelativeLayout> 

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

Result of this markup

+0

メニューの属性を使用する代わりにコンテンツを表示する –

答えて

8

RelativeLayoutであなたのNavigationViewを入れて、RelativeLayoutgravity "start"を設定し、最初の要素としてRelativelayout内部ImageViewを追加します。

そして、設定NavigationView背景透明

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

     <ImageView 
      android:id="@+id/drawer_bg" 
      android:src="@drawable/sidebar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:alpha="1" 
      android:scaleType="centerCrop"/> 
<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_main" 
    android:background="@android:color/transparent" 
    app:itemTextColor="@color/white" 
    app:itemIconTint="@color/white" 
    android:paddingTop="16dp" 
    style="@style/AppTheme.navigation" 
    app:menu="@menu/activity_main"> 

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

    </RelativeLayout> 

ようなので、あなたのレイアウトは今になります: - :背景のコンテナやフロントでのコンテナ私はでframeLayoutを使用する

 <?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/content_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
<RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="start"> 

      <ImageView 
       android:id="@+id/drawer_bg" 
       android:src="@drawable/sidebar" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:alpha="1" 
       android:scaleType="centerCrop"/> 
    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     android:background="@color/menuSemi" 
     app:itemTextColor="@color/white" 
     app:itemIconTint="@color/white" 
     android:paddingTop="16dp" 
     style="@style/AppTheme.navigation" 
     app:menu="@menu/activity_main"> 

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

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

を提供します。 NavigationViewのbackgroundとしてください。

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:background="@drawable/sidebar" 
    ... 
+0

ここに2つの問題があります。 1:画像が伸びる(縦横比が維持されません) 2:前面に半透明の色を設定できません – Fullhdpixel

0

背景

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:background="@drawable/sidebar" 

として画像を入れはラップコンテンツやマッチ親として

layout_widthとlayout_heightを設定しないでください。

または別々のナビゲーションヘッダファイルを持っている:あなたのNavigationViewで

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/view_container" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/nav_header_height" 
    android:gravity="bottom" 
    android:orientation="vertical" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark" 
    > 

    <ImageView 
     android:id="@+id/img_header_bg" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitXY" 
     android:src="@mipmap/ic_launcher" 
     /> 

...

0

だけでバックグラウンドにあなたの背景色

android:background="@color/menuSemi" 

を変更したDrawable

android:background="@drawable/YOUR_IMAGE_NAME" 

イメージを半透明にするには、アクティビティのコードを使用します。

YOUR_NAVIGATION_VIEW.getBackground().setAlpha(50); 
関連する問題