簡體   English   中英

在 Android 中,我有 2 個按鈕和一個文本。 我必須向左排列一個按鈕,中間放置文本,右側放置一個按鈕。 我怎樣才能做到這一點?

[英]In Android, I am having 2 Buttons and one Text. I have to arrange one button left, Text in Middle, and One Button in Right. How can I do this?

我正在使用線性布局水平。

在同一行中,我想在左側放置一個按鈕,將文本放在中間,將另一個按鈕放在右側。

如何實現這一點。?

相同的 XML 方法:

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

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".3"
        android:text="B1"
        />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".3"
        />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".3"
        android:text="B2"
        />
</LinearLayout>

如果要調整單個項目的寬度,可以更改 'android:layout_weight' 屬性

為了在 LinearLayout 中均勻分布元素,您需要使用weight 你可以這樣做:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/selector_default"
    android:orientation="horizontal"
    >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Text"
        />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Text"
        />
</LinearLayout>

暫無
暫無

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

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