2017-06-20 52 views
-1

你好傢伙我試圖改變我的導航抽屜項目的高度,我不知道如何,我已經到處尋找,盡我所能,但似乎沒有任何工作..如何自定義導航抽屜項目高度

Result

這是定製有有圖像下的文本,但我也想那個形象要大些我的抽屜式導航欄項目。

結果我仰視的是:

Menu Items

隨着高度的周圍75dp爲每一個項目,也是我希望菜單上的寬度短,得到的圖像在外觀。

我使用的是自定義佈局我當然項目,我命名爲:menu_item.xml

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

    <ImageView 
     android:layout_width="75dp" 
     android:layout_height="75dp" 
     android:src="@drawable/menu_icon"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Menu Title" 
     android:textSize="16sp" 
     android:textColor="@color/white" /> 

</LinearLayout> 

activity_main.xml中

<?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/app_bar_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:background="@color/colorPrimaryDark" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

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

最後的菜單; activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" > 

     <item 
      android:id="@+id/nav_profile" 
      app:actionLayout="@layout/menu_item"/> 
</menu> 

任何想法如何實現我想要什麼?在此先感謝

編輯:當我在我的自定義項目放大圖片如下:

這是menu_item.xml

<ImageView 
     android:layout_width="75dp" 
     android:layout_height="75dp" 
     android:src="@drawable/menu_icon"/> 

..我得到這樣的結果:

result2

正如你看到我的形象比項目更大..

+1

你可以自定義導航佈局 –

+0

要實現這一點,只需創建一個自定義佈局。 – RameshJaga

+0

@Anas你試過我的答案嗎? – Moulesh

回答

5

添加到您的styles.xml

<style name="NavigationTheme" parent="AppTheme"> 
     <item name="listPreferredItemHeightSmall">60dp</item><!-- menu item height- vary as u need --> 
    </style> 

,並在抽屜佈局SHD您的導航視圖包括主題LIK這

<android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:theme="@style/NavigationTheme" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 
+0

這是完美的工作,謝謝,還有一件事請,我如何居中該項目,因爲它停留在右側.. – Anas

+0

因爲你有修改你的項目xml,使線性佈局寬度匹配父&&和使佈局的文本和圖像視圖的重心水平居中 – Moulesh

+0

它沒有工作兄弟 – Anas

1

使自定義佈局

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:background="@color/white" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true"> 

    <include layout="@layout/custom_view"/> 

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

你能解釋更多嗎?我試圖做到這一點,幷包括我的'menu_item。xml'結果是一樣的 – Anas

+0

爲菜單項做一個custom_view,並通過findViewById簡單地獲取java文件中的每個項目,並在每個項目上設置點擊列表器 –

+0

謝謝你的工作,但它出現在導航的標題上菜單,並在菜單的左側,我希望它在中心 – Anas