简体   繁体   English

从linearLayout中的按钮删除侧面间距

[英]Remove side spacing from button in linearLayout

I have this layout designed so far 到目前为止,我已经设计了这种布局

在此处输入图片说明

Notice the side space around both the buttons with the name "save" that I have marked with red color, I want to remove that extra spacing. 注意两个按钮周围带有我已标记为红色的名称为“保存”的侧面空间,我想删除该多余的空间。

Both the buttons are wrapped inside linearlayout with orientation set to "vertical", I tried using layout_weight, marginLeft and marginRight but no success. 两个按钮都包装在linearlayout内,方向设置为“ vertical”,我尝试使用layout_weight, marginLeft and marginRight但没有成功。

Here's how the xml code looks like only for the buttons inside the linearlayout 这是xml代码的样子,仅适用于linearlayout内部的按钮

 <LinearLayout
        android:id="@+id/buttonSection"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"

        >
        <!--This layout is for save and change currency buttons-->


        <Button
             android:id="@+id/saveButton"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="Save"
             android:layout_weight="1"
          />

         <Button
             android:id="@+id/currencyButton"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="Save"
             android:layout_weight="1"
         />
    </LinearLayout>

Just for extra information the above linearlayout is wrapped inside one more linearlayout which controls the layout of full activity 只是为了获得更多信息,上述线性布局还包含在一个线性布局中,该线性布局控制整个活动的布局

EDIT 编辑

Full code for the main activity xml file 主要活动xml文件的完整代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@drawable/layer_list"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:weightSum="5"
    >

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="2"
        android:paddingTop="10dp"
        android:layout_marginBottom="30dp"
        >

        <LinearLayout
            android:id="@+id/leftCurrencySection"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"
            android:orientation="vertical"

            >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="GBP"
                android:textSize="30dp"
                android:textColor="#ffffff"
                android:textAllCaps="true"
                android:textStyle="bold"
                />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="0.00"
                android:textSize="30dp"
                android:textColor="#ffffff"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:background="@null"
                />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/rightCurrencySection"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="USD"
                android:textSize="30dp"
                android:textColor="#ffffff"
                android:textAllCaps="true"
                android:textStyle="bold"
                />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="0.00"
                android:textSize="30dp"
                android:textColor="#ffffff"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:background="@null"
                />

        </LinearLayout>

    </LinearLayout>
    <!--End of This layout is for typing currency values-->

    <LinearLayout
        android:id="@+id/buttonSection"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"

        >
        <!--This layout is for save and change currency buttons-->


            <Button
                android:id="@+id/saveButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Save"
                android:layout_weight="1"
                />

                <Button
                    android:id="@+id/currencyButton"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Save"
                    android:layout_weight="1"
                    />
    </LinearLayout>
    <!--End of  save and change currency buttons-->

    <!--Begin Layout for calculator begin-->

    <GridLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnCount="3"
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:layout_weight="1"
        >


        <Button android:text="1"

            />

        <Button android:text="2" />

        <Button android:text="3" />



        <Button android:text="4" />

        <Button android:text="5" />

        <Button android:text="6" />



        <Button android:text="7" />

        <Button android:text="8" />

        <Button android:text="9" />


        <Button android:text="." />

        <Button android:text="0" />

        <Button android:text="Del" />


    </GridLayout>
    <!--End Layout for calculator-->
    <!--End Layout for calculator End-->


</LinearLayout>

There are many people to thanks over here, who pointed out my mistake and also guided me to correct way of removing that extra space from either sides of the button. 这里有很多人要感谢,他们指出了我的错误,并引导我纠正了从按钮两侧去除多余空间的方法。

First I had to remove the padding from my parent linearlayout. 首先, 我必须从父线性布局中删除填充。 I removed paddingLeft and PaddingRight. 我删除了paddingLeft和PaddingRight。 Which removed the major part of the space. 从而删除了大部分空间。 Thanks to cricket_007 for pointing this out. 感谢cricket_007指出这一点。

Even after removing the space there was still a bit of space left out like 1 or 2dp. 即使删除了空间,仍然遗漏了一些空间,例如1或2dp。 This was apparently due to some default styling in android as pointed out by Doug Stevenson . 这显然是由于Doug Stevenson指出的android中的一些默认样式。

To remove this last form of space, I had to apply some sort of background mentioned by eclipse for example android:background="#00ff00" 要删除最后一种形式的空间,我必须应用eclipse提到的某种背景,例如android:background="#00ff00"

Applying the above attribute removed that extra space, this is due to 9patch in android. 应用上述属性删除了多余的空间,这是由于android中的9patch所致。 Learn more about it over here. 在这里了解更多信息。

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

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