Android线性布局按钮对齐

时间:2014-01-07 15:49:46

标签: android android-layout android-button

enter image description here

我想开发类似的线性布局。按钮1应该获得50%的布局空间,但不应该拉伸。我将指定按钮1的宽度。类似地,按钮2和按钮3应该获取剩余的50%的布局,我想从中心指定按钮2的左边填充,从布局的右端为按钮3填充右边。

我该怎么做?

我目前的代码:

 <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
     <Button 
       android:layout_height="30dp"
       android:layout_width="fill_parent"
       android:layout_weight="0.5"
       android:text="Button1"
        />
    <Button 
 android:layout_height="30dp"
android:layout_width="fill_parent"
android:layout_weight="0.75"
android:text="Button2"
    />

    <Button 
 android:layout_height="30dp"
android:layout_width="fill_parent"
android:layout_weight="0.75"
android:text="Button3"
    />
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

您可以创建两个布局,而不是对按钮进行加权,而每个布局都需要1个,因此它们将填充每个屏幕的50%..然后您可以根据需要操作每个布局中的按钮大小。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

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

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

    </LinearLayout>

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

        <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>

答案 1 :(得分:0)

你可以使用带有3个按钮的LinearLayout来做到这一点,只需在按钮上添加正确的重量,就像这样:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" android:layout_weight="4"/>

    <Button
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" 
        android:layout_weight="1"/>

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

</LinearLayout>