如何使线性布局的孩子之间的空间?

我通过编程方式将自定义视图添加到垂直线性布局中,我希望在视图之间有一些空间。我已经尝试添加:setPadding(0,1,0,1)到我的CustomView构造函数,但这似乎没有任何影响。任何建议吗?

有人指出我应该使用边距。因为我是动态添加视图,所以我需要从代码中设置边距(不是xml格式)。我相信这样做的方法是下面,但这是行不通的。

public class MyView extends View
{
public MyView (Context context)
{
super(context);


MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT,  LayoutParams.WRAP_CONTENT);
params.setMargins(0, 10, 0, 10);
setLayoutParams(params);

*编辑。我还尝试使用MarginLayoutParams作为参数,同时将视图添加到线性布局(如下所示)。这也没有起作用:

MarginLayoutParams params = new MarginLayoutParams(linearLayout.getLayoutParams());
linearLayout.setMargins(0, 10, 0, 10);
linearLayout.addView(view, params);
308679 次浏览

你应该在孩子们身上android:layout_margin<Side>。填充是内部的。

使用LinearLayout.LayoutParams代替MarginLayoutParams这是文档。

下面的示例仅以编程方式完成所需的工作。我使用的固定大小为(140,398)。

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(140, 398);
layoutParams.setMargins(24, 0, 24, 0);
layout.addView(button,layoutParams);

Android现在支持在视图之间添加空间视图。它可以从4.0 ICS开始使用。

API >= 11解决方案:

你可以把填充物整合成分隔线。如果你没有使用,只需要创建一个高的空可绘制对象,并将其设置为LinearLayout的分隔符:

    <LinearLayout
android:showDividers="middle"
android:divider="@drawable/empty_tall_divider"
...>...</LinearLayout>

empty_tall_divider.xml:

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


<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size
android:height="40dp"
android:width="0dp"/>
</shape>

如果你使用< >强ActionBarSherlock < / >强,你可以使用com.actionbarsherlock.internal.widget.IcsLinearLayout:

<com.actionbarsherlock.internal.widget.IcsLinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@drawable/list_view_divider"
android:dividerPadding="2dp"
android:showDividers="middle" >
...
</com.actionbarsherlock.internal.widget.IcsLinearLayout>

从API Level 14开始,你可以添加一个(透明的)分隔符绘制:

android:divider="@drawable/divider"
android:showDividers="middle"

它会帮你处理剩下的!

在子视图的布局中使用填充。

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/backage_text"
android:textColor="#999999"
>


</TextView>

backage_text.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">


<solid android:color="@color/white"/>
<corners android:radius="2dp"/>
<stroke
android:width="1dp"
android:color="#999999"/>
<padding
android:bottom="5dp"
android:left="10dp"
android:right="10dp"
android:top="5dp" />
</shape>

你可以得到父类LinearLayoutLayoutParams,并以这种方式应用到各个视图:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(8,8,8,8);
  • 注意setedges()将像素作为int数据类型。所以,在加值之前转换为dp
  • 上面的代码将把高度和宽度设置为wrap_content。你可以自定义它。

尝试在添加视图之后添加空间小部件,如下所示:

layout.addView(view)
val space = Space(context)
space.minimumHeight = spaceInterval
layout.addView(space)

如果你的布局包含文本容器中的标签。您可以在每个文本的末尾添加“\ n”来分隔一行并在元素之间留出空格 例子:< / p >

video?.text="Video NR1: ${obj.Titulo} \n"

你只需要用带有layout_weight的线性布局来包装项目。要将项目水平分开,请使用此选项

<LinearLayout
...
...
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">


// your item


</LinearLayout>
</LinearLayout>

你可以使用android:layout_weight="1"

< p >这样的: enter image description here < / p >