可以使用weight属性和gravity属性来实现让控件靠最右边。

  1. 使用weight属性

在LinearLayout中设置android:orientation="horizontal",并给需要靠右的控件设置android:layout_width="0dp",并且设置android:layout_weight="1",其他控件不设置android:layout_weight属性。这样靠右的控件会占据剩余的空间,从而靠右显示。

示例代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左边的控件"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="靠右的控件"/>

</LinearLayout>
  1. 使用gravity属性

在LinearLayout中设置android:orientation="horizontal",并将gravity属性设置为"end",这样所有控件都会靠右显示,但是靠右的控件可能会被覆盖。

示例代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="end">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左边的控件"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="靠右的控件"/>

</LinearLayout>
``
LinearLayout中怎么让控件靠最右边

原文地址: https://www.cveoy.top/t/topic/hugh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录