[英]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.