Android线性布局中按钮的右对齐,按钮并排
我想在线性布局中对齐视图右侧的按钮。 这导致了左边按钮的大小,我尝试了相对布局,因为我导致一个按钮在左边,另一个在右边。 我想要两个按钮并排,中间有10个间隙 请帮忙Android线性布局中按钮的右对齐,按钮并排,android,android-linearlayout,android-relativelayout,Android,Android Linearlayout,Android Relativelayout,我想在线性布局中对齐视图右侧的按钮。 这导致了左边按钮的大小,我尝试了相对布局,因为我导致一个按钮在左边,另一个在右边。 我想要两个按钮并排,中间有10个间隙 请帮忙 <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orient
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageButton
android:background="@null"
android:id="@+id/reply_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:src="@drawable/reply"
android:layout_gravity="left"/>
<Button
android:id="@+id/readmore_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@null"
android:text="Read more.."
android:textAllCaps="false"
android:textColor="@android:color/white"
android:textSize="14sp" />
</LinearLayout>
您可以将
android:layout\u weight=“2”
设置到您的容器中,并为每个子集合android:layout\u weight=“1”
和android:layout\u width=“0dp”
设置如下:
<?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"
android:layout_weight="2">
<ImageButton
android:background="@null"
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:layout_gravity="left"/>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="@null"
android:text="Read more.."
android:textAllCaps="false"
android:layout_weight="1"
android:textColor="@android:color/white"
android:textSize="14sp" />
</LinearLayout>
根据您的要求进行编辑:
<?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"
android:layout_weight="3">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="left"
android:layout_gravity="start"/>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:text="Read more.."
android:layout_weight="1"
android:textSize="14sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="right"
android:layout_weight="1"/>
</LinearLayout>
能否将线性布局的整个代码放在一个线性布局中
,子项指定的布局必须始终位于与方向正交的轴上。也就是说,对于水平线性布局
,它可以是顶部
、中心
(或中心/垂直
)或底部
。谢谢你的答复。我使用了第一个选项,但它没有同时选中两个按钮。它只允许对齐1个按钮。您能再次检查它吗?请“它只允许对齐1个按钮”-这不是很清楚,请帮助我更好地理解您-您可以添加一个屏幕截图,显示它现在的外观以及您希望它的外观吗?我已经编辑了我的问题,并在问题中添加了输出图像,所以您希望1行中有3个按钮?每个按钮都会占用相同的空间吗?@MikeM。感谢您添加此信息,通常我使用constraintLayout,所以我不知道。是的,我的意思是,我的错误
<?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"
android:layout_weight="3">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="left"
android:layout_gravity="start"/>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:text="Read more.."
android:layout_weight="1"
android:textSize="14sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="right"
android:layout_weight="1"/>
</LinearLayout>