簡體   English   中英

如何在同一行中的LinearLayout中對齊兩個TextView

[英]How to align two TextView in a LinearLayout in the same line

我在LinearLayout中有兩個TextView,我想在同一行中將它們一個對齊到左(或中心),一個對齊到右。 這個怎么做? 我嘗試使用重力,但他們忽略了它。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:text="TextView" />

</LinearLayout>

android:gravity用於設置視圖內部內容的重力。 但是,在您的情況下,寬度為wrap_content ,因此內容在文本視圖中無處可去。

使用帶有layout_width作為match_parentRelativeLayout 然后將android:layout_alignParentLeft="true"android:layout_alignParentRight="true"android:layout_alignParentRight="true"結合使用。

最簡單的方法是將LinearLayout更改為RelativeLayout

您可以使用android:layout_alignParentRight="true"android:layout_alignParentLeft="true" 或居中使用android:layout_centerInParent="true"

看到這里為什么重力不起作用

您正在使用gravity而不是您想要的layout_gravity 這篇文章應該有助於澄清差異

該文檔顯示了可用的屬性。

在第二個textview中將它與android:gravity一起使用或不一起使用,然后嘗試。

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:gravity="right"
        android:layout_weight="1" />
</LinearLayout>

如果LinearLayout為Vertical,則每行只能放置一個對象。 您可以使用RelativeLayout,也可以將LinearText Horizo​​ntal放置在一行中,其中包含textviews

例如

<LinearLayout vertical> <LinearLayout horizontal> <textview 1></> <textview 2></> </LinearLayout> </LinearLayout>

我用GridLayout修復了所有問題...這是bcos的最好的事情,您不需要對齊任何內容...只需將您想要的內容放入矩陣(行,列)...這將使您能夠可視化所有字段,准確包裝您的數據內容,橫向也完美!

暫無
暫無

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

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