android - 水平对齐两个按钮

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

<分区>

我有两个按钮,我想将它们水平放置,以便屏幕彼此并排,没有剩余空间,我知道我可以使用 linearlayout 并设置权重但我想要 relativelayout 因为我已经有权重在我的 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>

但是出现了取消按钮,那是因为我使用了“fill parent”,请问有什么解决办法?

最佳答案

试试这个

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

关于android - 水平对齐两个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14937523/

相关文章:

android - 使用 ItemTouchHelper.SimpleCallback 禁用在 RecyclerView 中的位置滑动

具有背景、源和文本的 Android ImageButton

"Hibernate"之后的 Android 布局问题

android - 在 gridview 项目之间显示一条细线

java - 将 dialogflow 依赖项添加到我的 android 应用程序后出现重复类错误

Android:使用 fragment 的更好方式

android - 自定义复选框可绘制的大小?

android - 当有很多 subview 时,如何使相对布局可滚动?

android - 如何在android XML中连接多个字符串?

java - Jacoco 在 Parceler 类上失败 - Android