I am programatically adding custom views to a vertical LinearLayout, and I would like there to be some space between the views. I have tried adding: setPadding(0, 1, 0, 1)
If your layout contain labels o some container for text. You can add at the end of each text "\n" to split a line and make space between elements.
Example:
video?.text="Video NR1: ${obj.Titulo} \n"
The API >= 11 solution:
You can integrate the padding into divider. In case you were using none, just create a tall empty drawable and set it as LinearLayout
's divider:
<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>
You should android:layout_margin<Side>
on the children. Padding is internal.
You can get the LayoutParams
of parent LinearLayout
and apply to the individual views this way:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(8,8,8,8);
Android now supports adding a Space view between views. It's available from 4.0 ICS onwards.
An easy way to do it dynamically is to add padding to the children. You can just set it using .setPadding() on the object to be added. This example is adding an ImageView to a LinearLayout:
LinearLayout userFeedLinearLayout = (LinearLayout) findViewById(R.id.userFeedLinearLayout);
imageView.setImageBitmap(bitmap);
imageView.setPadding(0, 30, 0, 30);
userFeedLinearLayout.addView(imageView);
The following image shows two ImageViews that have been added with padding: