簡體   English   中英

在Android的LinearLayout的角落放置ImageButtons

[英]Placement of ImageButtons in corners of LinearLayout in Android

我試圖將兩個ImageButton放置在LinearLayout的角落,如下圖所示:

在此處輸入圖片說明

我嘗試使用android:layout_gravity屬性,將兩個按鈕的值分別設置為leftright 但是,它們彼此相鄰顯示,如下圖所示:

在此處輸入圖片說明

如何將兩個圖像按鈕放在LinearLayout的角上? 我查詢了很多以前的答案並嘗試過,但似乎沒有任何效果。 如何放置圖像按鈕?

布局的XML在以下代碼段中:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|fill_horizontal"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/prev_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:contentDescription="@string/prev_button"
        android:src="@drawable/arrow_left"/>

    <ImageButton
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:contentDescription="@string/next_button"
        android:src="@drawable/arrow_right"/>
</LinearLayout>

使用RelativeLayout:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageButton
        android:id="@+id/prev_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/prev_button"
        android:src="@drawable/arrow_left"/>

    <ImageButton
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:contentDescription="@string/next_button"
        android:src="@drawable/arrow_right"/>
</RelativeLayout>

相對布局更適合將視圖放置在屏幕邊緣

還有另一種方法,而不是通過使用第三個View布局xml代碼來使用RelativeLayout

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

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <View
        android:layout_width="0dp"
        android:layout_weigth="1"
        android:layout_height="0.5dp"
        />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

這種方法比使用RelativeLayout更好,並使布局更平滑

暫無
暫無

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

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