2015-08-24 4 views
0

Я создал пользовательскую панель действий для своего приложения для Android с помощью класса панели инструментов. Он по-прежнему показывает кнопку меню слева от панели действий, которая при щелчке преобразуется в стрелку. Мне нужно сохранить этот значок, поскольку у меня есть боковое меню.Значок действия Значок Позиционирование Android

Проблема у меня в том, что другие значки, которые у меня есть, которые определены в моем XML-файле, не центрируются правильно.

Ниже приведен пример моей цели, но это не является прекрасным примером того, как есть 3 иконки в середине, а не 2.

Example of issue

Я знаю, что проблема может быть исправлена быстро, просто удалив значок меню Android влево, но я хотел бы сохранить его, если это возможно. Я также знаю, что мои значки центрируются, но центрируются по области справа от значка меню.

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" 
    android:elevation="4dp"> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:layout_marginTop="0dp" 
     android:gravity="center" 
     android:layout_alignParentTop="true" 
     android:id="@+id/top_bar" 
     android:weightSum="3"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:layout_weight="1" 
     > 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_gravity="center" 
      android:gravity="center" 
      android:layout_weight="1" 
      > 


      <ImageButton 
       android:id="@+id/icon1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:contentDescription="Icon1" 
       android:src="@drawable/icon1" 
       android:background="@null" 
       android:tint="#ffffffff" 
       android:layout_marginRight="10dp" 
       android:alpha="0.2"/> 


      <ImageButton 
       android:id="@+id/icon2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:contentDescription="@string/icon2" 
       android:src="@drawable/icon2" 
       android:background="@null" 
       android:tint="#ffffff" 
       android:maxHeight="25dp" 
       /> 

      <ImageButton 
       android:id="@+id/icon3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:contentDescription="icon3" 
       android:src="@drawable/icon3" 
       android:background="@null" 
       android:tint="#ffffffff" 
       android:layout_marginLeft="10dp" 
       android:alpha="0.2" /> 
     </LinearLayout> 



     <ImageButton 
      android:id="@+id/icon4" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:contentDescription="icon4" 
      android:src="@drawable/icon4'" 
      android:background="@null" 
      android:tint="#FFFFFF" 
      android:layout_marginTop="0dp" 
      android:layout_weight="1" 
      android:layout_gravity="right|center_vertical" 
      /> 

    </LinearLayout> 
</android.support.v7.widget.Toolbar> 

Должен ли я устанавливать положение этих значков программно и неподвижно? Или я вообще что-то пропущу?

Может кто-нибудь указать мне в правильном направлении, пожалуйста. Спасибо!

ответ

0

Я решил свою проблему!

Похоже, что внешний LinearLayout вызывал проблему наряду с внешними весами макета. Таким образом, в конце я удалил внешние LinearLayout и макеты Layout, обновил значок 4, чтобы иметь ширину содержимого обертки и родительскую высоту, и это сработало.

Для всех желающих, пожалуйста, ознакомьтесь с приведенным ниже кодом.

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="?android:attr/actionBarSize" 
android:elevation="4dp"> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" 
    android:gravity="center" 
    > 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:layout_weight="1" 
     > 


     <ImageButton 
      android:id="@+id/icon1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="icon1" 
      android:src="@drawable/icon1" 
      android:background="@null" 
      android:tint="#ffffffff" 
      android:layout_marginRight="10dp" 
      android:alpha="0.2"/> 


     <ImageButton 
      android:id="@+id/icon2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="@string/icon2" 
      android:src="@drawable/icon2" 
      android:background="@null" 
      android:tint="#ffffff" 
      android:maxHeight="25dp" 
      /> 

     <ImageButton 
      android:id="@+id/icon3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="icon3" 
      android:src="@drawable/icon3" 
      android:background="@null" 
      android:tint="#ffffffff" 
      android:layout_marginLeft="10dp" 
      android:alpha="0.2" /> 
    </LinearLayout> 



    <ImageButton 
     android:id="@+id/icon4" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:contentDescription="icon4" 
     android:src="@drawable/icon4" 
     android:background="@null" 
     android:tint="#FFFFFF" 
     android:layout_marginTop="0dp" 
     android:layout_marginRight="@dimen/activity_horizontal_margin" 
     android:layout_gravity="right|center_vertical" 
     /> 

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