簡體   English   中英

在布局底部創建4個ImageButton

[英]Create 4 ImageButtons at bottom of the layout

我正在嘗試在布局的底部顯示4個ImageButton。 我只能得到3個ImageButton。 第四個ImageButton不可見。 這是我的代碼。

我正在使用相對布局來顯示應用程序。

<ImageButton
    android:id="@+id/Button1"
    android:layout_weight="1.0"
    android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
    android:longClickable="true"
    android:layout_width="wrap_content"
    android:layout_height="75sp"
    android:background="@android:color/transparent"
    android:src="@drawable/imagebutton2"/>

 <ImageButton
    android:id="@+id/Button2"
    android:layout_gravity="bottom"
    android:layout_toRightOf="@+id/Button1"
    android:layout_width="wrap_content"
    android:layout_height="75sp"
    android:background="@android:color/transparent"
    android:src="@drawable/imagebutton1"
    android:layout_weight="1.0"
    android:layout_marginLeft="2dp"
    android:longClickable="true"/>

 <ImageButton
     android:id="@+id/Button3"
     android:layout_gravity="bottom" 
     android:layout_toRightOf="@+id/Button2"
     android:layout_height="75sp"
     android:layout_width="wrap_content"
     android:layout_weight="1.0"
     android:background="@android:color/transparent"
     android:src="@drawable/imagebutton1"
     android:layout_marginLeft="2dp"
     android:longClickable="true"/>
 <ImageButton
     android:id="@+id/Button4"
     android:layout_gravity="bottom" 
     android:layout_height="75sp"
     android:layout_width="wrap_content"
     android:layout_weight="1.0"
     android:background="@android:color/transparent"
     android:src="@drawable/imagebutton1"
     android:layout_marginLeft="2dp"
     android:longClickable="true"
     android:layout_alignParentRight="true"/>

將其放入帶權重的LinearLayout ,並將此LinearLayout與父對象的底部對齊,如下所示:

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

    <ImageButton
        android:id="@+id/ib1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/ib2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/ib3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/ib4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

請注意,此方法會降低性能。

首先,如果要繼續使用RelativeLayout作為其父級布局,則需要將其從ImageButton屬性中刪除:

android:layout_weight="1.0"

它在LinearLayout中使用,Lint應該向您發出警告(RelativeLayout中的無效布局參數)。

如果要在屏幕底部顯示4個按鈕,則需要添加

android:layout_alignParentBottom="true" 

在所有4個ImageButton中,我都嘗試了您提供的xml,但只有第一個按鈕顯示在底部。

最后但並非最不重要的一點是,如果您希望按鈕具有相同的大小以實現某些設計一致性,建議您將它們放置在具有以下內容的水平LinearLayout中:

android:layout_alignParentBottom="true"
android:layout_width="fill_parent"

並配置ImageButtons與

android:layout_width="0dp"
android:layout_weight="1.0" 

然后在您的RelativeLayout中包含該LinearLayout。

另一件事:由於您正在使用

android:layout_width="wrap_content"

對於您的ImageButton,您需要確保圖像不是太寬,否則某些按鈕可能不會顯示在屏幕上,因為第一個按鈕占用了太多空間,而最后一個按鈕則留在了右邊屏幕上的

附帶一提,我希望您不要嘗試制作iOS風格的下標簽欄,這在Android中是不受歡迎的,更多信息在這里 ...

祝你有個好的一天 !

在最后一個ImageButton中,您沒有:

android:layout_toRightOf="@+id/Button3"

如果您希望它位於底部,則將需要它。

我還建議您刪除一些代碼:

android:layout_gravity="bottom" 
android:layout_weight="1.0"

這僅適用於FrameLayoutLinearLayout

如果您想確定每個ImageButton都位於屏幕底部,請使用第一個按鈕使用的按鈕:

android:layout_alignParentBottom="true"

就像有些人說的那樣,在最后一個按鈕中,您沒有android:layout_toRightOf = "@id/Button3"因此它將位於布局的頂部。

我通常這樣做的另一種方法是:

android:layout_toRightOf = "@id/Button1"
android:layout_alignbottom = @id/Button1"

它將與button1的底部對齊。 我這樣做是因為有時此按鈕與另一個按鈕不一致,具體取決於布局。

暫無
暫無

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

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