2013-08-14 2 views
0

У меня есть три кнопки с таким размером, теперь мне нужно поставить четвертую кнопку ниже второй кнопки и выровнять центр четвертой кнопки с центром второй кнопки.android как выровнять кнопки?

У меня есть небольшая проблема с этим, любые идеи?

 

!---!---!---! 
! ! ! ! 
!---!---!---! 
    !-------! 
    !  ! 
    !-------! 

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

+0

Вы также можете опубликовать свой код макета? – Peshal

+0

первые три ответа действительно не помогли решить мою проблему, мой фактический макет достаточно сложный, с большим количеством компонентов и относительной компоновкой. Но спасибо всем, я просто меняю свой дизайн, а не пытаюсь решить эту проблему. У вас нет времени и терпения для разработки пользовательского интерфейса. – chotu123

ответ

0

Вот код

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

    <LinearLayout 
     android:id="@+id/top_buttons" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/button_1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="First" /> 

     <Button 
      android:id="@+id/button_2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Second" /> 

     <Button 
      android:id="@+id/button_3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Third" /> 
    </LinearLayout> 

    <Button 
     android:id="@+id/button_4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/top_buttons" 
     android:layout_centerHorizontal="true" 
     android:text="Forth" /> 
</RelativeLayout> 
0

Вы можете попробовать что-то вроде этого

<LinearLayout 
android:weightSum="3"> 
<Button 
    android:layout_weight="1"/> 
<Button 
    android:layout_weight="1"/> 
<Button 
    android:layout_weight="1"/> 
</LinearLayout> 
<LinearLayout 
android:weightSum="3"> 
<Button 
    android:layout_gravity="center_horizontal" 
    android:layout_weight="2"/> 
</LinearLayout> 

Очевидно заполнить требования необходимых тегов

+0

Могу ли я сделать это с относительной компоновкой? или он работает только с линейной компоновкой? – chotu123

+0

«layout_weight» является свойством детей LinearLayout, а свойство «weightSum» доступно только на LinearLayout – Selecsosi

0

Может быть, вы можете попробовать это:

Создать два LinearLayouts с одинаковой шириной. Поместите первые три кнопки в первый LinearLayout и четвертый во втором. Добавьте android:gravity="center" ко второй LinearLayout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <LinearLayout 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/layout1"> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="50dp" 
      android:layout_height="wrap_content" 
      android:text="B1" /> 
     <Button 
      android:id="@+id/button2" 
      android:layout_width="50dp" 
      android:layout_height="wrap_content" 
      android:text="B2" /> 
     <Button 
      android:id="@+id/button3" 
      android:layout_width="50dp" 
      android:layout_height="wrap_content" 
      android:text="B3" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/layout1" 
     android:gravity="center" 
     > 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button2" 
      android:layout_gravity="center" 
      /> 

    </LinearLayout> 

</RelativeLayout> 
0

Пожалуйста, попробуйте этот код, это решит вашу проблему. рад помочь

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Button" /> 
</LinearLayout> 

</LinearLayout>