How to make space between LinearLayout children?

后端 未结 13 502
一整个雨季
一整个雨季 2020-11-30 23:46

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)

相关标签:
13条回答
  • 2020-12-01 00:25

    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" 
    
    0 讨论(0)
  • 2020-12-01 00:27

    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>
    
    0 讨论(0)
  • 2020-12-01 00:28

    You should android:layout_margin<Side> on the children. Padding is internal.

    0 讨论(0)
  • 2020-12-01 00:28

    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);
    
    • Take care that setMargins() take pixels as int data type.So, convert to dp before adding values
    • Above code will set height and width to wrap_content. you can customise it.
    0 讨论(0)
  • 2020-12-01 00:29

    Android now supports adding a Space view between views. It's available from 4.0 ICS onwards.

    0 讨论(0)
  • 2020-12-01 00:35

    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:

    0 讨论(0)
提交回复
热议问题