Как выровнять пробелы между кнопками? - PullRequest
1 голос
/ 29 февраля 2012

Мне нужно выровнять пробелы между четырьмя кнопками.Есть ли способ сделать это?Проблема в том, что первая и последняя кнопки должны быть около края, а оставшееся пространство должно быть поровну разделено для оставшихся двух кнопок и трех пробелов.

layout / main.xml:

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

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

        <Button
            android:id="@+id/setupButtonA1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_arrow_up"
            android:enabled="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_weight="1">

        <Button
            android:id="@+id/setupButtonA2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_arrow_down"
            android:layout_weight="0.5"
            android:enabled="true" />

        <Button
            android:id="@+id/setupButtonB1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_arrow_up"
            android:layout_weight="0.5"
            android:enabled="false" />
    </LinearLayout>

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

        <Button
            android:id="@+id/setupButtonB2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_arrow_down"
            android:enabled="false" />
    </LinearLayout>
</LinearLayout>

Спасибо за все ваши ответы, правильное решение было:

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

    <Button
        android:id="@+id/setupButtonA1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_arrow_up"
        android:enabled="true" />

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <Button
        android:id="@+id/setupButtonA2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_arrow_down"
        android:enabled="true" />

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <Button
        android:id="@+id/setupButtonB1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_arrow_up"
        android:enabled="false" />

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <Button
        android:id="@+id/setupButtonB2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_arrow_down"
        android:enabled="false" />
</LinearLayout>

Ответы [ 5 ]

4 голосов
/ 29 февраля 2012

Вы можете использовать приведенный ниже код, в который я вставил пустые виды с весом 1, установленным во всех элементах управления в linearlayout.

 <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >
<Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button" android:layout_weight="1"/>
 <View
            android:id="@+id/v2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button" android:layout_weight="1"/>
 <View
            android:id="@+id/v2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button" android:layout_weight="1"/>
 <View
            android:id="@+id/v2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
            <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button" android:layout_weight="1"/>
    </LinearLayout>
0 голосов
/ 29 февраля 2012

Я не стану писать пример кода, как это сделали другие, вместо этого я просто объясню, как я это сделаю.

Вся полоса кнопок (ширина fill_parent, высота wrap_content) будетRelativeLayout.Добавьте первую кнопку, установите layout_alignParentLeft = true. Добавьте следующую кнопку, установите layout_alignParentRight = true (это будет последняя кнопка на полосе). Добавьте LinearLayout (fill_parent, wrap_content), toLeftOf = lastButton, toRightOf = firstButton.это должно поместить это в середину двух кнопок.Установите его ориентацию в горизонтальное положение.Внутри этого LinearLayout добавьте два новых LinearLayouts (layout_width = "0dip" , layout_height = wrap_content).Установите layout_weight = 1.Установите гравитацию = центр.Внутри этих двух LinearLayouts разместите оставшиеся кнопки.

Нет необходимости устанавливать weightSum для любого макета.Все кнопки имеют (wrap_content, wrap_content) или желаемый размер.

0 голосов
/ 29 февраля 2012

Попробуйте android:layout_marginLeft="28dp" в вашем

android:id="@+id/setupButtonA2"  and       
     android:id="@+id/setupButtonB1"

Измените значение dp в соответствии с вашим макетом

0 голосов
/ 29 февраля 2012

Используйте android: weightSum = "4", это должно определить в макете, это отсутствует в вашем коде. и определите android: layout_weight = "1" для каждой кнопки

 <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"  
         android:weightSum="4"> /////------ i made changes here

        <Button 
            android:id="@+id/setupButtonA2" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:background="@drawable/button_arrow_down" 
            android:layout_weight="1" 
            android:enabled="true" /> 

        <Button 
            android:id="@+id/setupButtonB1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:background="@drawable/button_arrow_up" 
            android:layout_weight="1" 
            android:enabled="false" /> 
       <Button 
            android:id="@+id/setupButtonB1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:background="@drawable/button_arrow_up" 
            android:layout_weight="0.5" 
            android:enabled="false" /> 

       <Button 
            android:id="@+id/setupButtonB1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:background="@drawable/button_arrow_up" 
            android:layout_weight="0.5" 
            android:enabled="false" /> 

    </LinearLayout> 
0 голосов
/ 29 февраля 2012

Попробуйте android:layout_weight="0"

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