简体   繁体   English

默认情况下添加了“线性布局”边距。 如何避免呢? Android的

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

I have created 4 buttons in linear layout. 我创建了4个线性布局的按钮。 This is my xml. 这是我的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. 但是在布局上方,为所有3个按钮添加了所有边距。 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 在按钮中,设置android:layout_width="0dp"而不是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. 这是因为您已为水平LinearLayout按钮赋予了weight属性,每个Button将水平填充根布局的1/3。 And if you are using a vertical Linear Layout , you should set android:layout_height="0dp" 如果您使用的是垂直Linear Layout ,则应设置android:layout_height="0dp"

Hope this will help you. 希望这会帮助你。

These are the shadow by default of Android Button Widget. 默认情况下,这些是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 但是,如果要强制将一个按钮与另一个按钮附加在一起,则根据需要将以下属性添加到每个buttons

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

Note : Increase or decrease the value ( Currently -5dp ) of the properties as it suits. 注意:根据需要增加或减小属性的值( 当前为-5dp )。

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

android:background="#8c9eff"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式更改线性布局上边距android - Change linear layout top margin programmatically android 如何将填充或边距设置为线性布局? - how to set padding or margin to linear layout? 如何以编程方式设置线性布局边距(Xamarin) - How to set Linear Layout Margin Programmatically (Xamarin) android-添加到线性布局更改的项目间距 - android - spacing of items added to linear layout changes 线性布局中的按钮边距 - Margin of buttons in linear layout 在webview中缩放图像以适合父布局高度,因此默认情况下不滚动。 (机器人) - Scale image in webview to fit inside the parent layout height, so there is no scroll by default. (Android) 如何设置在线性布局Android中动态添加的充气布局中心 - how to set inflate layout center which added dynamically in linear layout Android Android - 代表点击的线性布局默认 onClickListener? - Android - Linear Layout default onClickListener that delegates clicks? android-将按钮默认样式应用于线性布局 - android - apply button default style to linear layout 在Android布局中查看默认情况下以前为黑色的文本现在突然显示为灰色。 无法解决改变了什么 - View text in Android layout that was previously black by default now suddenly grey by default. Cannot work out what's changed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM