2016-12-27 69 views
0

enter image description here我如何填補drawerLayout

我設計佈局headerView,想添加圖像圖標在其右側,就像照片。

問題是,它會顯示圖像中的空間,即使我刪除圖像佈局,空間仍然存在。

我嘗試更改其佈局寬度的父佈局或子佈局,任何參數(如數學父項或包裝內容)或給出dp。

我試過官方的演示來解決它,但它沒有奏效。

任何人都可以教我如何做到這一點?

enter image description here enter image description here

我nav_heafer.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:background="#66CDAA" 
    android:orientation="horizontal"> 
    <!--the left navigationView--> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp"> 
      <!--the left icon--> 
      <ImageView 
       android:id="@+id/imageView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="16dp" 
       app:srcCompat="@drawable/ic_grid_on_black_24dp" /> 
      <!--the left textView--> 
      <TextView 
       android:id="@+id/logIn" 
       android:textColor="@android:color/background_light" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/pageLogIn" 
       android:background="?android:attr/selectableItemBackground" 
       android:clickable="true"/> 
      <!--the left button--> 
      <Button 
       android:id="@+id/logOut" 
       android:text="@string/logOut" 
       android:layout_width="65dp" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 
    </LinearLayout> 
    <!--I just want a outstanding icon over here,on the right side of navigationView--> 
    <!--I try add the LinearLayout,but it shows strange--> 
</LinearLayout> 


    [1]: https://i.stack.imgur.com/hQy1d.png 
    [2]: https://i.stack.imgur.com/E9jyu.png 

我的MainActivity 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" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="false" 
    tools:context="com.example.user.taiwandigestionsociety_v11.MainActivity"> 


    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#FFFFFF"> 



     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolBar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:theme="@style/ToolBarStyle" 
      app:layout_scrollFlags="scroll|enterAlways" 
      android:background="@color/colorPrimary"> 
     </android.support.v7.widget.Toolbar> 

     <FrameLayout 
      android:id="@+id/mainFrame" 
      android:layout_gravity="end" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

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


    <!--control navigationView width--> 
<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="230dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="@android:color/white" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header" 
    app:menu="@menu/drawer_menu"> 
</android.support.design.widget.NavigationView> 


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

回答

0

沒事,我終於明白你的問題,並solved.This應該給你的東西如下所示。 navigation panel

讓我們開始吧!

首先,想要移除圖標背後的白色背景,請在導航視圖中設置透明背景,如下所示。

<android.support.design.widget.NavigationView 
    android:id="@+id/navigation_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 

    <!-- it works as transparent background color --> 
    android:background="@android:color/transparent" 

    app:headerLayout="@layout/navigation_header_support" /> 

您的自定義標題,因爲你希望你的關閉圖標是在您的導航面板的最右邊,你應該使用相對佈局父。所以我們可以根據父母對齊該關閉圖標。這次設置背景顏色給你的相對父母佈局的直接孩子,就像我爲線性佈局所做的那樣。

爲了相對佈局內適合的圖標,讓右線性佈局一些餘量,所以剩菜右側空白空間會被你的圖標所佔據。設置您的圖標的寬度與從右側將您的線性佈局推送到的空間相同。在這裏,我從右側留下32dp的餘量,並設置我的圖標的寬度以匹配它。

對於該圖標的正確位置,我使用了以下屬性。你應該調整你的。

android:layout_alignParentRight="true" 
android:layout_alignParentTop="true" 

的nav_header.xml

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



     <LinearLayout 
      android:paddingTop="64dp" 

      <!-- background color --> 
      android:background="@color/blue_2" 

      android:layout_marginRight="32dp" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <TextView 
       android:textColor="@android:color/white" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="@dimen/activity_horizontal_margin" 
       android:drawableLeft="@drawable/ic_pin_primary" 
       android:drawablePadding="@dimen/activity_horizontal_margin" 
       android:drawableStart="@drawable/ic_pin_primary" 
       android:gravity="center_vertical" 
       android:typeface="normal" 
       android:textSize="18sp" 
       android:text="Location" 
       android:textStyle="bold" /> 

      <!-- Other items go here --> 
     </LinearLayout> 


     <ImageView 
      android:background="@color/blue_2" 
      android:layout_alignParentRight="true" 
      android:layout_marginTop="64dp" 
      android:layout_alignParentTop="true" 
      android:src="@drawable/ic_pencil" 
      android:layout_width="32dp" 
      android:layout_height="32dp" /> 

    </RelativeLayout> 
+0

我將nav_header.xml更改爲match_parent,它仍然存在:( –

+0

thsnks @xFighter我重置了NavigationView的寬度,它成功了,我現在想在抽屜的右邊添加圖像,它讓我感到困擾 –

+0

你想讓你的圖像佔據右側的所有空間嗎? – xFighter

0

沒事,這不是一個回答你的問題。但是你可以改善你的佈局以及性能。然後根據此更改您問題中的導航佈局,並再次更新您的問題。那麼它可以更具可讀性並且易於理解你的問題。所以任何人都可以找到很容易,而不是使用與水平方向的圖標和TextView的,因爲我如下圖所示,你可以做,而不是一個線性佈局回答

您目前的執行情況:

<LinearLayout 
     android:layout_marginTop="30dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <ImageView 
      android:id="@+id/imageView9" 
      android:layout_marginLeft="10dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      app:srcCompat="@drawable/ic_grid_on_black_24dp" /> 

     <TextView 
      android:id="@+id/newInformation" 
      android:textSize="15dp" 
      android:layout_marginLeft="10dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/newInformation" 
      android:textColor="@android:color/background_light"/> 

    </LinearLayout> 

現在它改成這樣的:

<TextView 
    android:id="@+id/person_address" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 

    <!-- this is where your icon goes --> 
    android:drawableLeft="@drawable/ic_grid_on_black_24dp" 
    android:drawableStart="@drawable/ic_grid_on_black_24dp" 

    <!-- your icon's padding --> 
    android:drawablePadding="@dimen/activity_horizontal_margin" 

    android:gravity="center_vertical" /> 

請改變你的佈局弄成這個樣子,那麼我可以試着回答你的問題。

+0

謝謝你的熱情和抱歉,太慢太迴應,所以你想我粘貼yout textView替換圖像(id:imageView9)和textView和linearlayout權利? 我改變它,但它只是崩潰,沒有任何錯誤信息。我該怎麼做? –

+0

我認爲你不能讓我正確。你不需要linearLayout和ImageView來相互顯示它們。你可以簡單地使用我在示例中展示的方法。 – xFighter

+0

我真的不明白,所以你的意思是我不需要使用兩個linearlayout嗎? 只需使用一個imageIcon通過在一個佈局中填充即可控制它,它將顯示我想要的內容,導航視圖在右側顯示一個圖標。 是這樣嗎? –