繁体   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