LinearLayout将孩子放在右侧

Vih*_*rma 4 layout android android-linearlayout layout-gravity android-relativelayout

我试图在水平方向的线性布局中使用textview和按钮.textview应出现在开头,按钮应出现在结尾处.我认为将重力直接按到按钮就可以了,但按钮不会移动到右侧.我在想我是否应该使用相对布局?

在此输入图像描述

<\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>
Run Code Online (Sandbox Code Playgroud)

Fan*_*mas 9

我的方式(使用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>
Run Code Online (Sandbox Code Playgroud)

看看我如何明确地将TextView与Parent的左侧对齐,将Button与父级的右侧对齐

然后,您可以通过设置以下方式将TextView垂直居中于RelativeLayout:

android:layout_centerVertical="true"
Run Code Online (Sandbox Code Playgroud)

在TextView本身


Pra*_*asa 6

尝试以下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>
Run Code Online (Sandbox Code Playgroud)


VGM*_*VGM 5

有一种更简洁的方法可以使用 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>
Run Code Online (Sandbox Code Playgroud)