我可以更改导航抽屉中图标和项目标题之间的距离吗?

问题描述:

我我的导航菜单上显示下一个项目:我可以更改导航抽屉中图标和项目标题之间的距离吗?

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:id="@+id/nav_locate" 
    android:icon="@mipmap/ic_add_location_black_24dp" 
    android:title="Localizare" /> 

    <item android:id="@+id/nav_propose" 
android:icon="@mipmap/ic_landscape_black_24dp" 
android:title="Obiective" /> 

    <item android:id="@+id/nav_propose" 
android:icon="@mipmap/ic_settings_black_24dp" 
android:title="Setari" /> 

</menu> 

但我不喜欢的图标和文字之间的距离。要大。我可以在这两件事之间设定我自己的距离吗?

因为我不认为有任何直接的方法来做到这一点,你可以直接膨胀自定义布局导航抽屉。从自定义布局,你可以做任何你想要的。这个想法是,你做出包含ImageView和TextView的列表项,并且你可以直接给它们设置距离(边距或填充)。

这里是DrawerLayout

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- Main content --> 
    <RelativeLayout 
     android:id="@+id/main_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <!-- Insert any views you want as main content --> 
    </RelativeLayout> 

    <!-- Navigation drawer --> 
    <RelativeLayout 
     android:id="@+id/navigation_drawer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" /> 

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

这里的你如何虚增您的自定义布局

RelativeLayout drawer = (RelativeLayout) findViewById(R.id.navigation_drawer); 
View drawerContent = getLayoutInflater().inflate(R.layout.drawer_content, null); 
drawerContent.setLayoutParams(new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.MATCH_PARENT, 
    RelativeLayout.LayoutParams.MATCH_PARENT)); 
drawer.addView(drawerContent); 

的例子现在你创建drawer_content.xmlres/layout并进行布局,如你所愿。 这里,你可以使

<?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="match_parent" 
    android:orientation="vertical"> 

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

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="8dp" 
      android:src="@drawable/ic_menu1" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:text="Menu 1" /> 
    </LinearLayout> 

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

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="8dp" 
      android:src="@drawable/ic_menu2" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:text="Menu 2" /> 
    </LinearLayout> 

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

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="8dp" 
      android:src="@drawable/ic_menu3" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:text="Menu 3" /> 
    </LinearLayout> 

</LinearLayout> 
+0

菜单以灰色窗口画的drawer_content.xml的例子。并在这里得到错误android:src =“@ drawable/ic_menu1”/> –

+0

你应该在你的drawable目录中有ic_menu1。关于灰色窗口,您可以直接将背景设置为LinearLayout或带有android:id =“@ + id/navigation_drawer”的容器RelativeLayout。 – HendraWD