简体   繁体   English

LinearLayout中按钮的边距不起作用

[英]Margin of Button inside LinearLayout not working

In my project, I'd like to have two Button elements in a LinearLayout without space between them. 在我的项目中,我想在LinearLayout有两个Button元素,并且它们之间没有空格。

I've tried to set padding and margin to 0dp , to remove style and to remove ScrollView but nothing works. 我试图将paddingmargin设置为0dp ,以删除style并删除ScrollView但是没有任何效果。 I can't figure out a way to remove the space between buttons. 我想不出一种方法来删除按钮之间的空间。 Do you have some ideas? 你有什么主意吗?

My XML is: 我的XML是:

     <android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- The first child in the layout is for the main Activity UI-->

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


</LinearLayout>

<!-- Side navigation drawer UI -->
<ScrollView
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="left|start"
    android:background="#ffeeeeee"
    android:clickable="true"
    android:fillViewport="true"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:orientation="vertical" android:padding="0dp" >

        <Button
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/agenda"
            android:drawableLeft="@drawable/agenda_icon"
            android:textAllCaps="false"
            android:gravity="left|center_vertical"
            android:drawablePadding="5dp"
            android:layout_margin="0dp"
            android:padding="0dp"
            />

        <Button
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/day"
            android:drawableLeft="@drawable/agenda_icon"
            android:textAllCaps="false"
            android:gravity="left|center_vertical"
            android:drawablePadding="5dp"
            android:layout_margin="0dp"
            android:padding="0dp"

            />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/month"
            android:drawableLeft="@drawable/agenda_icon"
            android:textAllCaps="false"
            android:gravity="left|center_vertical"
            android:drawablePadding="5dp"
            android:layout_margin="0dp"
            android:padding="0dp"
            style="?android:attr/borderlessButtonStyle"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray"/>
    </LinearLayout>



</ScrollView>


</android.support.v4.widget.DrawerLayout>

But the result is (I've touch the last two buttons to show the button background): 但是结果是(我触摸了最后两个按钮以显示按钮背景): 结果图片

Setting padding of each Button to 0 (@aga answer) doesn't solve my problem :( 将每个Button的padding设置为0(@aga答案)不能解决我的问题:( 在此处输入图片说明

Try changing the color of the button, because the default interface of the button which is native to android is center-fitted to make it look cool. 尝试更改按钮的颜色,因为android原生的按钮默认界面居中,以使其看起来很酷。

Change the background color of the button to match the color of the layout in which it is placed, then we will get the real size of the button. 更改按钮的背景颜色以匹配其放置的布局的颜色,然后我们将获得按钮的实际大小。 In your case 就你而言

android:background="#000"

Use this since it does not use the style it may get rid of some issues by setting the width to a hard coded size and by uses default paddings 使用此选项,因为它不使用样式,可以通过将宽度设置为硬编码大小并使用默认填充来摆脱一些问题

<Button
                    android:id="@+id/btn_overtime"
                    android:layout_width="310sp"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal|center_vertical"
                    android:text="Overtime"
                    android:textSize="20sp"
                    android:textStyle="bold" />

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

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