Возникли проблемы при центрировании двух виджетов по горизонтали - PullRequest
2 голосов
/ 10 июня 2011

У меня есть макет, в котором я хочу, чтобы два виджета ToggleButton центрировались горизонтально;не прямо рядом друг с другом, но как будто каждый был сосредоточен в своей половине зрения.So, I'm wanting them to look like this.

But instead I get this.

Вот код для моего макета:

`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<LinearLayout android:id="@+id/pics"
        android:layout_weight="2"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:padding="8dp"
        android:layout_alignParentTop="true" >
    <ImageView android:id="@+id/prefix"
            android:layout_weight="1"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:padding="4dp"/>
    <ImageView android:id="@+id/suffix"
            android:layout_weight="1"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:padding="4dp" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        orientation="horizontal" >
    <LinearLayout android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            orientation="horizontal" >
        <ToggleButton android:id="@+id/lockPrefix"
                android:textOn="Locked"
                android:textOff="Unlocked"
                android:checked="false"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal" />
    </LinearLayout>
    <LinearLayout android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            orientation="horizontal" >
        <ToggleButton android:id="@+id/lockSuffix"
                android:textOn="Locked"
                android:textOff="Unlocked"
                android:checked="false"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_gravity="center_horizontal" />
    </LinearLayout>
</LinearLayout>
<LinearLayout android:id="@+id/toolbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_below="@id/pics"
        android:padding="8dp" >
    <Button android:id="@+id/mixButton"
            style="@style/DefaultButton"
            android:layout_height="wrap_content"
            android:layout_width="90dp"
            android:text="@string/main_mixButton"
            android:onClick="mixButtonClick" />
    <Button android:id="@+id/exitButton"
            style="@style/DefaultButton"
            android:layout_height="wrap_content"
            android:layout_width="90dp"
            android:text="@string/main_exitButton"
            android:onClick="exitButtonClick" />
</LinearLayout>
</LinearLayout>`

(РЕДАКТИРОВАТЬ: сделать XML читаемым)

Ответы [ 2 ]

2 голосов
/ 10 июня 2011

Вам нужно поместить каждую кнопку в LinearLayout, установить layout_width этих макетов на fill_parent, layout_weight на 1 и gravity на center_horizontal. Надеюсь, это поможет!

0 голосов
/ 10 июня 2011

Попробуйте это, для кнопки переключения, удаляя вложенные линейные макеты.(Я заменил с 24-й строки, я полагаю)

<LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        orientation="horizontal" 
        android:layout_weight="1"
>
      <ToggleButton android:id="@+id/lockPrefix"
               android:textOn="Locked"
               android:textOff="Unlocked"
               android:checked="false"
               android:layout_width="0dip"
               android:layout_weight="0.5"
               android:layout_height="wrap_content"
               android:layout_gravity="center_horizontal" />

        <ToggleButton android:id="@+id/lockSuffix"
                android:textOn="Locked"
                android:textOff="Unlocked"
                android:checked="false"
                android:layout_width="0dip"
                android:layout_weight="0.5"
                android:layout_height="wrap_content" 
                android:layout_gravity="center_horizontal" />

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