eclipse 如何在 LinearLayout 中隔开项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6103960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:20:11  来源:igfitidea点击:

How to space out items in a LinearLayout?

javaandroideclipseandroid-layoutandroid-linearlayout

提问by Sheehan Alam

I want to create a toolbar that looks like:

我想创建一个看起来像的工具栏:

enter image description here

在此处输入图片说明

How can I do this in XML? Here is what mine looks like currently:

如何在 XML 中执行此操作?这是我目前的样子:

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/toolbarLinearLayout" android:background="@color/solid_yellow">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/replyButton" android:text="Reply"></Button>
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RT" android:id="@+id/rtButton"></Button>
        <Button android:id="@+id/dmButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DM"></Button>
    </LinearLayout>

回答by actionshrimp

Looks like you might want the padding attribute of the LinearLayout, e.g.

看起来你可能想要 LinearLayout 的 padding 属性,例如

android:padding="5dip"

To get the items to each take up the same amount of space, use layout_weight, e.g.

要让每个项目占用相同的空间,请使用 layout_weight,例如

 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/toolbarLinearLayout" android:background="@color/solid_yellow">
    <Button android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/replyButton" android:text="Reply"></Button>
    <Button android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="wrap_content" android:text="RT" android:id="@+id/rtButton"></Button>
    <Button android:id="@+id/dmButton" android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="wrap_content" android:text="DM"></Button>
</LinearLayout>

As long as all the elements have the same weight they should take up the same space. layout_width="fill_parent" will ensure the whole bar is filled.

只要所有元素具有相同的权重,它们就应该占据相同的空间。layout_width="fill_parent" 将确保整个栏被填满。

回答by Ted Hopp

Define your toolbar something like this:

像这样定义你的工具栏:

<LinearLayout ...>
    <Button android:id="@+id/replyButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ...
        />
    <View
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        />
    <Button ...
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <!-- etc. -->
</LinearLayout>

All the extra space will be allocated to the transparent views between the buttons.

所有额外的空间将分配给按钮之间的透明视图。

回答by anjaneya

Add android:layout_margin="some value" for item in linearlayout.This worked for me.

为线性布局中的项目添加 android:layout_margin="some value"。这对我有用。