Неправилно подравняване на бутоните в RelativeLayout

Използвах няколко RelativeLayouts в рамките на едно LinearLayout. И RelativeLayout, използван за бутони, причини неправилен външен вид, както се вижда по-долу:

въведете описание на изображението тук

Това е моят .xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/wood"
    android:orientation="vertical"
    android:padding="25dp" >

<RelativeLayout
       android:id="@+id/relative"
       android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/button1"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/button2"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>

 <RelativeLayout
       android:id="@+id/relative2"
       android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    <Button
        android:id="@+id/btn5"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

    <Button
        android:id="@+id/btn8"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

    <Button
        android:id="@+id/btn7"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/btn8"
        android:text="Button" />

    <Button
        android:id="@+id/btn6"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/btn7"
        android:text="Button" />

</RelativeLayout>

</LinearLayout>

Как мога да подравня бутоните си правилно и добре, без да използвам Grid View, тъй като изисква допълнителна библиотека и API 14 и по-горе? Благодаря ти.


person Umit Kaya    schedule 20.05.2014    source източник
comment
Здравей, Umitk! Извинете за безпокойството! Имате ли нещо против да отворите отново 4-годишния въпрос. Има няколко алтернативни отговора, които липсват на въпроса. Тези отговори заслужават облекчение и могат да бъдат особено полезни за начинаещите в AOSP. Благодаря предварително!   -  person Onik    schedule 14.09.2018


Отговори (1)


Вместо RelativeLayouts използвайте LinearLayouts хоризонтално ориентиран:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/wood"
    android:orientation="vertical"
    android:padding="25dp" >

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

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

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

<Button
    android:id="@+id/button3"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<Button
    android:id="@+id/button4"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>

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

<Button
    android:id="@+id/btn5"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<Button
    android:id="@+id/btn8"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<Button
    android:id="@+id/btn7"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<Button
    android:id="@+id/btn6"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>
</LinearLayout>
person Onik    schedule 20.05.2014
comment
…и това е лоша идея. Вероятно е без значение, но вие използвате 1 ниво на влагане, което не е необходимо. - person Martin Marconcini; 18.02.2017