簡體   English   中英

刪除線性布局下方按鈕的多余空間

[英]Remove extra space of the buttons below Linear Layout

在此處輸入圖片說明

按鈕下方有這個額外的空間,我不知道為什么在那里。 我竭盡所能,尋找任何遇到這種問題的人,可惜我什么都沒找到。 我要么要刪除多余的空間和/或將其降低到完全對齊的狀態。

這是我的xml文件

<RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout 
            android:id="@+id/button_layout"
            android:layout_alignParentTop="true"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#262626"
            android:gravity="center">
            <Button 
                android:id="@+id/receipts_button"
                android:background="@android:color/transparent"
                android:drawableTop="@drawable/preview_save_icon"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            <Button android:id="@+id/capture_button"
                android:background="@android:color/transparent"
                android:drawableTop="@drawable/preview_upload_icon"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            <Button android:id="@+id/settings_button"
                android:background="@android:color/transparent"
                android:drawableTop="@drawable/preview_discard_icon"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </LinearLayout>

有什么理由要使用按鈕而不是ImageView? 您正在使用android:drawableTop ,它將圖像放置在頂部,並在底部留出一些空間以顯示文本! 因為您不需要文本,所以用ImageView替換文本並將其設置為源圖像更容易。 因此,它將以視圖為中心:

  <LinearLayout 
            android:id="@+id/button_layout"
            android:layout_alignParentTop="true"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#262626"
            android:gravity="center">
            <ImageView 
                android:id="@+id/receipts_button"
                android:background="@android:color/transparent"                
                android:src="@drawable/ic_launcher"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            <ImageView android:id="@+id/capture_button"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_launcher"                
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            <ImageView android:id="@+id/settings_button"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_launcher"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </LinearLayout>

嘗試一下,實際上,當您設置android:gravity =“ center”時,它屬於布局本身,當您說layout_gravity時,它將應用於布局的子級。

android:layout_gravity="center_vertical"

首先,擁有許多嵌套布局不是一個好習慣。 在您的情況下,您可以刪除LinearLayout或RelativeLayout。 除此之外,盡管發布整個xml會有幫助,但我建議刪除LinearLayout中的android:layout_alignParentTop="true" ,並可能添加類似以下內容: android:layout_gravity="bottom"

  • 刪除android:drawableTop屬性,並在android:background屬性中提供Button圖像。 android:drawableTop會將您的圖片放在頂部。
  • 設置layout_gravity屬性center_vertical的按鈕

     <LinearLayout android:id="@+id/button_layout" android:layout_alignParentTop="true" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#262626" android:gravity="center"> <Button android:id="@+id/receipts_button" android:background="@drawable/preview_save_icon" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1"/> <Button android:id="@+id/capture_button" android:background="@drawable/preview_upload_icon" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1"/> <Button android:id="@+id/settings_button" android:background="@drawable/preview_discard_icon" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1"/> </LinearLayout> 

游戲玩家正確地說明了原因 除此之外,如果您要繼續使用Buttons則只需將android:drawableTop替換為android:drawableLeft ,您應該會得到想要的東西。

希望幫助。

暫無
暫無

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

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