Android 如何只指定一次 LinearLayout 元素之间的间距?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12454940/
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
How to specify spacing between elements of LinearLayout only once?
提问by Dude
I recently ran into a problem again that I already had several times in the last years.
我最近再次遇到了一个问题,过去几年我已经遇到过几次了。
LinearLayout
is a very convenient layout manager
. But what I totally miss is the possibility to add a certain space between the elements (like padding) in a single XML tag.
LinearLayout
是很方便的layout manager
。但我完全想念的是在单个 XML 标记中的元素之间添加特定空间(如填充)的可能性。
What I mean by one tag is, that I can define in the declaration of the LinearLayout the spacing between the elements (e.g. in a vertical LinearLayout the vertical space between two elements in this layout).
我所说的一个标签的意思是,我可以在 LinearLayout 的声明中定义元素之间的间距(例如,在垂直 LinearLayout 中,此布局中两个元素之间的垂直间距)。
I know that I can do it by adding the XML tag android:layout_marginTop
or something similar to every element in the LinearLayout.
我知道我可以通过添加 XML 标记android:layout_marginTop
或类似于 LinearLayout 中的每个元素的东西来做到这一点。
But I would like to be able to define it in only one point, as the spacing is the same for all elements.
但我希望能够仅在一点上定义它,因为所有元素的间距都相同。
Does anybody know an easy way to do this (not implementing a custom LinearLayout or something like that)? I prefer a solution that works directly in XML without the need for coding.
有没有人知道一种简单的方法来做到这一点(不实现自定义 LinearLayout 或类似的东西)?我更喜欢直接在 XML 中工作而无需编码的解决方案。
采纳答案by chris-tulip
the way that is recommended is to apply a style to all the elements in the linear layout
推荐的方法是将样式应用于线性布局中的所有元素
android:style="@style/mystyle"
<style name="mystyle">
<item name="android:layout_marginTop">10dp</item>
... other things that your elements have in common
</style>
回答by Zon
Set custom transparent drawable as a divider for your layout:
将自定义透明 drawable 设置为布局的分隔线:
<LinearLayout
android:showDividers="middle"
android:divider="@drawable/divider">
New drawable resource in drawables folder (divider.xml):
drawables 文件夹中的新可绘制资源 (divider.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android">
<size
android:width = "0dp"
android:height = "16dp"/>
</shape>
回答by Watercayman
@Chris-Tulip's answer really helped me - with good practice too.
@Chris-Tulip 的回答真的对我有帮助 - 也有很好的练习。
For those of you who might be getting an Eclipse error about missing a resource identifier for "Style" in package android, like I did, you don't need to add the android namespace.
对于那些可能会收到 Eclipse 错误的人来说,在包 android 中缺少“样式”的资源标识符,就像我一样,您不需要添加 android 命名空间。
So, android:style="xx" brings up the error, while style="xx" is correct. Funky, but for anyone having that error, this might help.
因此, android:style="xx" 会出现错误,而 style="xx" 是正确的。时髦,但对于任何有该错误的人来说,这可能会有所帮助。
回答by Yuriy
You should add android:layout_marginTop
or android:layout_marginLeft
to element which must have indent. Depends on android:orientation
of your LinearLayout
.
您应该添加android:layout_marginTop
或添加android:layout_marginLeft
到必须有缩进的元素。取决于android:orientation
您的LinearLayout
.
回答by dbm
You could define your single item "prototype" in a separate xml file and then inflate the items from that file, dynamically in code and insert them into your linear layout.
您可以在单独的 xml 文件中定义单个项目“原型”,然后在代码中动态地从该文件中扩展项目并将它们插入到您的线性布局中。
You would then define the spacing on the actual item, not the parent LinearLayout, (as a android:layout_marginTop
for example) and that spacing would be applied to all your items as you inflate them.
然后,您将定义实际项目上的间距,而不是父 LinearLayout,(android:layout_marginTop
例如),并且在您膨胀它们时,该间距将应用于所有项目。
EDIT:
编辑:
container.xml:
容器.xml:
<LinearLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Your items will be added here -->
</LinearLayout>
item.xml:
项目.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is my child" />
</LinearLayout>
MyActivity.java:
我的Activity.java:
// Put this in a suitable place in your Java code, perhaps
// in "onCreate" or "onResume" depending on where and how
// you initialize your view. You can, of course inflate
// any number of instances of the item and add them to
// your parent LinearLayout.
LayoutInflater inflater = LayoutInflater.from(context);
View item = inflater.inflate(R.layout.item, null, false);
LinearLayout container = findViewById(R.id.parent);
container.addView(view);
I haven't put an effort in testing the code, but it "should" work as is :-)
我没有努力测试代码,但它“应该”按原样工作:-)