默认情况下添加线性布局边距。怎么避免呢? Android的

时间:2015-12-11 06:14:32

标签: android android-layout android-xml

我创建了4个线性布局按钮。这是我的xml。

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3">
    <Button android:id="@+id/button1"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:text="Account"
        />
    <Button android:id="@+id/button2"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:text="History"
        />
    <Button
        android:id="@+id/button3"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:text="Settings"
        />
</LinearLayout>

但是上面的布局为所有3个按钮增加了所有边的边距。为什么默认情况下会添加边距?如何避免?

4 个答案:

答案 0 :(得分:3)

enter image description here

试试这个,它就像你想要的那样,

 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <Button android:id="@+id/button1"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:text="Account"
        android:background="#80ffdd10"
        />
    <Button android:id="@+id/button2"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:background="#800aad10"
        android:text="History"
        />
    <Button
        android:id="@+id/button3"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:text="Settings"
        android:background="#8c9eff"
        />
</LinearLayout>

答案 1 :(得分:0)

在按钮中,设置android:layout_width="0dp"而不是wrap_content

这是因为您已将weight属性赋予水平LinearLayout中的按钮,每个Button将水平填充根布局的1/3。 如果您使用垂直Linear Layout,则应设置android:layout_height="0dp"

希望这会对你有所帮助。

答案 2 :(得分:0)

这些是Android Button Widget默认的阴影。但是,如果要强制附加一个按钮,则根据需要将以下属性添加到每个buttons

android:layout_marginRight="-5dp"
android:layout_marginLeft="-5dp"

注意:增加或减少适合的属性值(当前-5dp )。

答案 3 :(得分:0)

您可以使用自定义背景,它将解决您的问题

android:background="#8c9eff"