簡體   English   中英

LinearLayout將孩子放在右側

[英]LinearLayout put child at the right side

我試圖在水平方向的線性布局中使用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>

我的方式(使用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與Parent的左側對齊,將Button與父級的右側對齊

然后,您可以通過設置以下方式將TextView垂直居中於RelativeLayout:

android:layout_centerVertical="true"

在TextView本身

嘗試以下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>

有一種更簡潔的方法,使用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>

有兩種方法:

1)您可以使用RelativeLayout ,您可以在其中拖動您想要的Button

2)您可以使用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="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>

請改用此布局..

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

使用android:layout_gravity="end"到您的線性布局,將所有元素移動到右側

這里不依賴於使用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更有效。 但對我來說,在如此簡單的布局中去找一個親戚總覺得有點過分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM