【问题标题】:Align two buttons horizontally [duplicate]水平对齐两个按钮[重复]
【发布时间】:2013-02-18 13:31:16
【问题描述】:

我有两个按钮,我想将它们水平放置,以便屏幕彼此并排,没有剩余空间,我知道我可以使用线性布局并设置权重,但我想要相对布局,因为我已经有权重在我的 XML 中,因此将两个权重放在一起会很糟糕。

我试过这样

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_selector" >

    <Button
        android:id="@+id/b_orderMeal_send"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="4dip"
        android:background="@drawable/button_selector"
        android:clickable="true"
        android:focusable="true"
        android:text="@string/b_send"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/b_orderMeal_cancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="4dip"
        android:background="@drawable/button_selector"
        android:clickable="true"
        android:focusable="true"
        android:text="@string/b_cancel"
        android:textColor="#FFFFFF" />
</RelativeLayout>

但只是出现了取消按钮,那是因为我使用了“填充父项”,请问有什么解决方案?

【问题讨论】:

  • 使用水平方向的线性布局
  • 请问如何让按钮占据所有可用空间?
  • 我会发布答案等待

标签: android android-layout android-xml android-button


【解决方案1】:

试试这个

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

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

</LinearLayout>

【讨论】:

  • 请告诉你,我不想使用线性布局,也不想给用户权重,
  • 他给了我答案,谢谢你+1
  • 这对我很有用。
【解决方案2】:

尝试添加到您的代码中

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_selector" >


    <Button
            android:id="@+id/b_orderMeal_send"

           android:layout_toLeftOf="@+id/view"
    ....
    />

     <View 
            android:id="@+id/view"
            android:layout_height="1dp"
            android:layout_width="0dp"
            android:layout_centerHorizontal="true"
            />

     <Button
            android:id="@+id/b_orderMeal_cancel"

            android:layout_toRightOf="@+id/view"
    ...
    />
</RelativeLayout>

【讨论】:

  • 按钮的宽度应该保持“填充父项”?
  • 是的 android:layout_width="fill_parent"
  • 我会在 10 分钟后接受您的回答
猜你喜欢
  • 2020-12-20
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
  • 2016-04-07
  • 2012-08-31
  • 2014-07-27
  • 2016-02-20
  • 1970-01-01
相关资源
最近更新 更多