简体   繁体   English

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

[英]Can I change distance between icon and item title in Navigation Drawer?

I display next items on my navigation menu: 我在导航菜单上显示下一个项目:

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

But I don't like distance between icon and text. 但是我不喜欢图标和文本之间的距离。 To big. 要大。 Can I set my own distance between these two things? 我可以在这两件事之间设定自己的距离吗?

Since i don't think there is any direct ways to do that, you can inflate custom layout directly to navigation drawer. 由于我认为没有任何直接方法可以执行此操作,因此您可以将自定义布局直接添加到导航抽屉中。 From the custom layout, you can do anything you want. 在自定义布局中,您可以执行任何所需的操作。 The idea is, you make list items that contain ImageView and TextView, and you can set the distance(margin or padding) directly to them. 这个想法是,您制作包含ImageView和TextView的列表项,并且可以直接设置它们之间的距离(边距或填充)。

Here is the example of DrawerLayout 这是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>

Here how you inflate your custom layout 在这里,您如何膨胀自定义布局

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);

Now you create drawer_content.xml inside res/layout and make the layout as you wish. 现在,您可以在res/layout内创建drawer_content.xml并根据需要进行布局。 Here is the example of drawer_content.xml that you can make 这是您可以创建的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中更改导航抽屉标题图标图像 - To change navigation drawer title icon image in Android 如何更改导航抽屉及其项目android studio的图标 - How can I change the icon of the navigation drawer and its item android studio 如何在Android中以编程方式设置图标和导航抽屉菜单项标题之间的边距? - How to programmatically set the margin between icon and title of menu item of Navigation Drawer in Android? 如何以编程方式更改导航抽屉中的菜单标题 - How can I change the menu title in navigation drawer programmatically 如何覆盖抽屉导航按钮的行为并更改图标? - How can i override drawer navigation button behaviour and change the icon? 如何在ActionBarActivity中更改图标和标题之间的距离 - How change distance between icon and title in ActionBarActivity 如何更改包含导航抽屉中菜单的项目的项目标题字体颜色 - How do I change Item title font color for an Item that contains a menu in Navigation Drawer 更改导航抽屉的图标 - Change Icon Of Navigation Drawer 如何在Android导航抽屉中更改标题箭头图标 - How to change the title arrow icon in android navigation drawer 如何在Android的导航抽屉中更改项目标题背景? - How to change Item title background in the navigation drawer in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM