2016-10-10 6 views
0

Мне нужно сделать элементы из меню (Навигационный ящик с полной шириной) влево, совмещенном с их значками, с текстом текста под этим, и каждый элемент должен находиться в квадрат, обертывающий его содержимое, поэтому элементы вместе будут похожи на выровненную башню. Потому что я сделал для них собственный макет xml.Как выровнять пользовательский NavigationView с макетом пользовательских элементов меню до самого левого

Но я не могу сделать пользовательский макет, который я сделал для элементов меню, чтобы выровнять по левому краю экрана. (Ящик меню открывается справа налево). Мой activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginEnd="0dp" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="end"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="end" 
     android:layout_marginEnd="0dp" 
     android:fitsSystemWindows="true" 
     android:layoutDirection="rtl"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <LinearLayout 
       android:id="@+id/header" 
       android:layout_width="match_parent" 
       android:layout_height="172dp" 
       android:background="@drawable/my_custom_background" 
       android:orientation="horizontal"> 
      </LinearLayout> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:id="@+id/icon" 
        android:layout_width="20dp" 
        android:layout_height="20dp" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentStart="true" 
        android:src="@drawable/ic_menu_camera"/> 

       <TextView 
        android:id="@+id/title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true" 
        android:layout_toEndOf="@+id/icon" 
        android:layout_toRightOf="@+id/icon" 
        android:gravity="center_vertical" 
        android:text="Line 1" 
        android:textColor="#000" 
        android:textStyle="bold"/> 

       <TextView 
        android:id="@+id/subTitle" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/title" 
        android:layout_toRightOf="@+id/icon" 
        android:text="Line 2"/> 

      </RelativeLayout> 
     </LinearLayout> 

    </android.support.design.widget.NavigationView> 

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

Your_custom_menu_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="start|top" 
       android:orientation="vertical" 
       android:layoutDirection="rtl" 
       android:background="@drawable/border_set" 
       android:padding="10dip"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:paddingLeft="0dp" 
     android:paddingRight="0dp" 
     android:src="@drawable/ic_menu_camera" 
     /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:paddingStart="0dp" 
     android:text="your_text" 
     android:textColor="@color/black" 
     android:textSize="9dp" /> 
</LinearLayout> 

activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layoutDirection="ltr"> 

    <group android:checkableBehavior="single"> 
     <item 
      android:id="@+id/nav_camera" 
      android:icon="@drawable/ic_menu_camera" 
      android:title="" 
      app:showAsAction="never" 
      app:actionLayout="@layout/your_custom_menu_layout"/> 
     <item 
      android:id="@+id/nav_gallery" 
      android:icon="@drawable/ic_menu_gallery" 
      android:title="" 
      app:showAsAction="never" 
      app:actionLayout="@layout/your_custom_menu_layout"/> 
     <item 
      android:id="@+id/nav_slideshow" 
      android:icon="@drawable/ic_menu_slideshow" 
      android:title="Slideshow" 
      app:showAsAction="always" 
      app:actionLayout="@layout/your_custom_menu_layout"/> 
     <item 
      android:id="@+id/nav_manage" 
      android:icon="@drawable/ic_menu_manage" 
      android:title="Tools" 
      app:showAsAction="always" 
      app:actionLayout="@layout/your_custom_menu_layout"/> 
    </group> 

    <item android:title="Communicate"> 
     <menu> 
      <item 
       android:id="@+id/nav_share" 
       android:icon="@drawable/ic_menu_share" 
       android:title="Share" 
       app:actionLayout="@layout/your_custom_menu_layout"/> 
      <item 
       android:id="@+id/nav_send" 
       android:icon="@drawable/ic_menu_send" 
       android:title="Send" 
       app:actionLayout="@layout/your_custom_menu_layout"/> 
     </menu> 
    </item> 

</menu> 

What I get:

What I need:

ответ

0

В вашем Your_custom_menu_layout.xml,

В Родитель Linear Layout Удалить

android:padding="10dip" 

Это даст 10 дп отступы со всех сторон. Таким образом, вы не можете видеть свой TextView. Как только вы удалите это, вы можете получить желаемый результат. Всего наилучшего.

+0

Да, спасибо Ajith. Текст теперь показан. Но мне все равно нужно выровнять список в самом левом углу. – snewtMs

+0

Добро пожаловать. Всего наилучшего. –

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

  • Нет связанных вопросов^_^