【问题标题】:LinearLayout put child at the right sideLinearLayout 将孩子放在右侧
【发布时间】:2023-04-06 14:03:01
【问题描述】:

我试图在水平方向的线性布局中拥有一个文本视图和一个按钮。文本视图应该出现在开头,按钮应该出现在结尾。我认为将重力赋予按钮可以解决问题,但按钮不会移动到右侧。我在想我是否应该使用相对布局?

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

    <TextView
        android:id="@+id/productPriceTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rs 3579.0" 
        />

    <Button
        android:id="@+id/buyNowButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Buy Now" />
<\/LinearLayout>

【问题讨论】:

    标签: android layout android-linearlayout android-relativelayout layout-gravity


    【解决方案1】:

    我的方式(使用RelativeLayout):

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:id="@+id/productPriceTextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="Rs 3579.0"
        />
        <Button
            android:id="@+id/buyNowButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Buy Now"
        />
    </RelativeLayout>
    

    查看我如何将 TextView 显式对齐到父级的左侧,并将按钮显式对齐到父级的右侧

    然后,您可以通过设置将 TextView 在 RelativeLayout 中垂直居中:

    android:layout_centerVertical="true"
    

    在 TextView 本身中


    较新的操作系统版本可能更喜欢这个:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:id="@+id/productPriceTextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:text="Rs 3579.0"
        />
        <Button
            android:id="@+id/buyNowButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:text="Buy Now"
        />
    </RelativeLayout>
    

    【讨论】:

    【解决方案2】:

    试试下面的xml:

       <?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="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/productPriceTextView1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="Rs 3579.0"
            />
    
        <Button
            android:id="@+id/buyNowButton1"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="Buy Now" />
    </LinearLayout>
    

    【讨论】:

    • 这会使按钮的大小变大,我认为相对布局适合这种情况。
    • 非常感谢。根据我的需要,它工作正常。
    【解决方案3】:

    有一种更简洁的方法可以做到这一点,使用 LinearLayout :只需将左侧元素的宽度设置为 0,权重设置为 1,然后在 wrap_content 上设置右侧元素的宽度。就是这样!

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/productPriceTextView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Rs 3579.0" 
            />
    
        <Button
            android:id="@+id/buyNowButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Buy Now" />
    </LinearLayout>
    

    【讨论】:

    • 它运行良好,是否对性能有任何影响
    • 我不知道,因为它是一个简单的线性布局。
    【解决方案4】:

    有两种方法:

    1) 您可以使用RelativeLayout,在其中您可以将Button 拖到您想要的位置..

    2) LinearLayout 可以使用 weight 属性。

    <?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="wrap_content"
    android:orientation="horizontal">
    
    <TextView
        android:id="@+id/productPriceTextView1"
        android:layout_width="0dp"
        android:layout_weight="0.8"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="Rs 3579.0"
        />
    
    <Button
        android:id="@+id/buyNowButton1"
        android:layout_width="0dp"
        android:layout_weight="0.2"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Buy Now" />
    </LinearLayout>
    

    【讨论】:

      【解决方案5】:

      改用这个布局..

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
      
      <TextView
      android:id="@+id/productPriceTextView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Rs 3579.0"
      android:layout_centerVertical="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true" />
      
      
      
      <Button
      android:id="@+id/buyNowButton1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Buy Now"
      android:layout_alignParentTop="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" />
      
      </RelativeLayout>
      

      【讨论】:

        【解决方案6】:

        在线性布局中使用android:layout_gravity="end" 将所有元素向右移动

        【讨论】:

          【解决方案7】:

          这里另一个不依赖于使用RelativeLayout 的简单解决方案是使用layout_weight 在两个元素之间设置一个空的view,以确保它填充空白空间。

          <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content" 
          android:orientation="horizontal">
          
            <TextView
                android:id="@+id/productPriceTextView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Rs 3579.0"/>
          
            <view
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:layout_weight="1" />
          
            <Button
                android:id="@+id/buyNowButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="Buy Now" />
          </LinearLayout>
          

          这样你就可以通过只使用LinearLayout 来实现你想要的,现在,我不确定这是否比使用RelativeLayout 更有效。但对我来说,在如此简单的布局中寻找亲戚总是有点矫枉过正。

          【讨论】:

            【解决方案8】:

            只需添加 0dp 宽度和 1 权重的空视图

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                android:orientation="horizontal">
            
                <TextView
                    android:id="@+id/productPriceTextView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Rs 3579.0" 
                    />
                 <View
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_weight="1"/>
            
                <Button
                    android:id="@+id/buyNowButton1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    
                    android:text="Buy Now" />
            </LinearLayout>
            

            【讨论】:

              猜你喜欢
              • 2015-03-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-30
              • 2017-07-28
              • 2017-12-28
              • 1970-01-01
              相关资源
              最近更新 更多