[英]Can I change distance between icon and item title in Navigation 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>
但是我不喜歡圖標和文本之間的距離。 要大。 我可以在這兩件事之間設定自己的距離嗎?
由於我認為沒有任何直接方法可以執行此操作,因此您可以將自定義布局直接添加到導航抽屜中。 在自定義布局中,您可以執行任何所需的操作。 這個想法是,您制作包含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);
現在,您可以在res/layout
內創建drawer_content.xml
並根據需要進行布局。 這是您可以創建的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.