【发布时间】:2016-03-07 03:17:53
【问题描述】:
到目前为止我已经设计了这个布局
请注意我用红色标记的名称为“保存”的两个按钮周围的侧面空间,我想删除多余的间距。
两个按钮都包裹在线性布局中,方向设置为“垂直”,我尝试使用layout_weight, marginLeft and marginRight,但没有成功。
这是 xml 代码的外观,仅适用于线性布局内的按钮
<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>
【问题讨论】:
-
你能发布你正在使用的整个 xml 文件吗?如果这是一个片段,那么你的主要活动 xml 文件?
-
从父布局容器中移除内边距
-
在你的两个 Button 属性 android:layout_width="match_parent";
-
@eclipse1203:我已经更新了完整的代码。 To-cricket_007:我尝试从主父布局容器中删除填充,但仍有少量空间留出
-
按钮的默认样式包括在每个边缘上的一些填充,以便按钮在彼此相邻放置时不会相互碰撞或与其他小部件碰撞。对于我使用过的每个 Android 版本都是如此。如果您希望按钮(和许多其他小部件)能够与其他小部件齐平呈现,则必须为小部件提供自己的背景样式(通常是 9-patch)。
标签: android xml android-layout