简体   繁体   中英

Linear Layout margin added by default. How to avoid that? Android

I have created 4 buttons in linear layout. This is my 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>

But above layout adding margins all sides for all 3 buttons. Why is that margins is added by default? How to avoid that?

在此处输入图片说明

Try this,it works like you want,

 <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>

In button, set android:layout_width="0dp" rather than wrap_content

It's because you have given weight attribute to buttons in horizontal LinearLayout , each Button will fill 1/3 of the root layout horizontally. And if you are using a vertical Linear Layout , you should set android:layout_height="0dp"

Hope this will help you.

These are the shadow by default of Android Button Widget. However If you want to attach one button with another forcefully then add the following properties to each buttons as per needed

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

Note : Increase or decrease the value ( Currently -5dp ) of the properties as it suits.

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

android:background="#8c9eff"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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