2016-11-28 1 views
0

отобразить следующие пункты в моем меню навигации:Можно ли изменить расстояние между значком и заголовком элемента в навигационном ящике?

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

Но мне не нравится расстояние между иконкой и текстом. К большому. Могу ли я установить свое расстояние между этими двумя вещами?

ответ

0

Поскольку я не думаю, что есть какие-либо прямые способы сделать это, вы можете раздувать пользовательский макет непосредственно в ящике навигации. Из пользовательского макета вы можете делать все, что хотите. Идея заключается в том, что вы создаете элементы списка, которые содержат ImageView и TextView, и вы можете установить расстояние (margin или padding) непосредственно к ним.

Вот пример 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> 
+0

окрашена в сером окне. И получите ошибку здесь. Android: src = "@ drawable/ic_menu1" /> –

+0

Вы должны иметь ic_menu1 внутри вашего ресурса. И о сером окне вы можете установить фон непосредственно для содержимого LinearLayout или контейнера RelativeLayout с помощью 'android: id =" @ + id/navigation_drawer "' – HendraWD

Смежные вопросы