简体   繁体   English

删除线性布局下方按钮的多余空间

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

在此处输入图片说明

There's this extra space below the buttons, and I dont know why is it there. 按钮下方有这个额外的空间,我不知道为什么在那里。 I tried everything and looked for anyone who have had this kind of problem and sadly i found nothing. 我竭尽所能,寻找任何遇到这种问题的人,可惜我什么都没找到。 It's either I want to remove the extra space and/or lower it down perfectly aligned. 我要么要删除多余的空间和/或将其降低到完全对齐的状态。

Here's my xml file 这是我的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>

Is there any reason you are using a button instead of ImageView? 有什么理由要使用按钮而不是ImageView? You are using android:drawableTop which will position your image in the top part and leave some space in the bottom for text! 您正在使用android:drawableTop ,它将图像放置在顶部,并在底部留出一些空间以显示文本! As you don't want text, I'ts easier to replace it with ImageView and set the image as source. 因为您不需要文本,所以用ImageView替换文本并将其设置为源图像更容易。 So it will be centered on the view: 因此,它将以视图为中心:

  <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"

First of all it is not a good practice to have many nested Layouts. 首先,拥有许多嵌套布局不是一个好习惯。 In your case you could remove either the LinearLayout or the RelativeLayout. 在您的情况下,您可以删除LinearLayout或RelativeLayout。 Beyond that, although it would be helpfull to post the whole xml, I would suggest removing the android:layout_alignParentTop="true" in the LinearLayout, and maybe adding something like: android:layout_gravity="bottom" 除此之外,尽管发布整个xml会有帮助,但我建议删除LinearLayout中的android:layout_alignParentTop="true" ,并可能添加类似以下内容: android:layout_gravity="bottom"

  • Remove android:drawableTop attribute and give Button image in android:background attribute. 删除android:drawableTop属性,并在android:background属性中提供Button图像。 android:drawableTop will position your image in the top part. android:drawableTop会将您的图片放在顶部。
  • Set the layout_gravity attribute to center_vertical for the buttons 设置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> 

Reason for this is correctly told by gameower . 游戏玩家正确地说明了原因 In addition to that, if you want to continue using Buttons then you just need to replace android:drawableTop by android:drawableLeft and you should get what you want. 除此之外,如果您要继续使用Buttons则只需将android:drawableTop替换为android:drawableLeft ,您应该会得到想要的东西。

Hope that hepls. 希望帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM