Как сделать так, чтобы кнопки Android вели себя как панель инструментов? - PullRequest
0 голосов
/ 15 сентября 2011

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

Я изо всех сил пытаюсь получить правильное расположение:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:baselineAligned="true" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="1">
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_gravity="center" android:id="@+id/textView1"></TextView>


    <LinearLayout android:id="@+id/linearLayout2" android:orientation="horizontal" android:layout_width="match_parent" android:layout_gravity="bottom" android:layout_height="187dp">
        <Button android:id="@+id/Button03"  android:layout_height="match_parent" android:text="Button" android:layout_width="match_parent"></Button>
        <Button android:id="@+id/Button02" android:layout_height="match_parent" android:text="Button"  android:layout_width="match_parent"></Button>
        <Button android:id="@+id/Button01" android:layout_height="match_parent" android:text="Button"  android:layout_width="match_parent"></Button>
        <Button android:id="@+id/button1"  android:layout_height="match_parent" android:text="Button" android:layout_width="match_parent"></Button>
    </LinearLayout>


</LinearLayout>

Ответы [ 2 ]

1 голос
/ 15 сентября 2011

У тебя было много чего происходит. Во-первых, это должен быть fill_parent, а не match_parent. Ваш внешний LinearLayout вы также настроили как горизонтальный, а не вертикальный. Также, как упомянул CommonsWare, используйте вес 0dp + 1, чтобы равномерно распределять объекты. Вот измененный макет, который, я думаю, близок к тому, что вы хотите:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:baselineAligned="true" android:layout_height="fill_parent" android:orientation="vertical" android:weightSum="1">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_gravity="center" android:id="@+id/textView1"></TextView>

<LinearLayout android:id="@+id/linearLayout2" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_gravity="bottom" android:layout_height="187dp">
    <Button android:id="@+id/Button03" android:layout_height="fill_parent" android:layout_width="0dp" android:layout_weight="1" android:text="Button"></Button>
    <Button android:id="@+id/Button02" android:layout_height="fill_parent" android:text="Button" android:layout_width="0dp" android:layout_weight="1"></Button>
    <Button android:id="@+id/Button01" android:layout_height="fill_parent" android:text="Button" android:layout_width="0dp" android:layout_weight="1"></Button>
    <Button android:id="@+id/Button04" android:layout_height="fill_parent" android:text="Button" android:layout_width="0dp" android:layout_weight="1"></Button>
</LinearLayout>

</LinearLayout>

Просто быстрое исправление, и вам придется больше подправлять, но это идея.

1 голос
/ 15 сентября 2011

Используйте android:layout_width="0dip" и android:layout_weight="1" для всех ваших Button виджетов, чтобы они были одинакового размера в пределах горизонтали LinearLayout.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...