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

时间:2016-11-28 15:31:10

标签: android menu navigation icons drawer

我在导航菜单上显示下一个项目:

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

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

1 个答案:

答案 0 :(得分:0)

由于我认为没有任何直接的方法可以做到这一点,您可以将自定义布局直接扩展到导航抽屉。从自定义布局中,您可以执行任何操作。我们的想法是,您创建包含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.xml内创建res/layout并根据需要进行布局。 以下是您可以制作drawer_content.xml的示例

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