[英]Remove side spacing from button in linearLayout
到目前为止,我已经设计了这种布局
注意两个按钮周围带有我已标记为红色的名称为“保存”的侧面空间,我想删除该多余的空间。
两个按钮都包装在linearlayout内,方向设置为“ vertical”,我尝试使用layout_weight, marginLeft and marginRight
但没有成功。
这是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>
只是为了获得更多信息,上述线性布局还包含在一个线性布局中,该线性布局控制整个活动的布局
编辑
主要活动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>
这里有很多人要感谢,他们指出了我的错误,并引导我纠正了从按钮两侧去除多余空间的方法。
首先, 我必须从父线性布局中删除填充。 我删除了paddingLeft和PaddingRight。 从而删除了大部分空间。 感谢cricket_007指出这一点。
即使删除了空间,仍然遗漏了一些空间,例如1或2dp。 这显然是由于Doug Stevenson指出的android中的一些默认样式。
要删除最后一种形式的空间,我必须应用eclipse提到的某种背景,例如android:background="#00ff00"
应用上述属性删除了多余的空间,这是由于android中的9patch所致。 在这里了解更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.