How to make space between LinearLayout children?

后端 未结 13 501
一整个雨季
一整个雨季 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:15

    The sample below just does what you need programatically. I have used a fixed size of (140,398).

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(140, 398);
            layoutParams.setMargins(24, 0, 24, 0);
            layout.addView(button,layoutParams);
    
    0 讨论(0)
  • 2020-12-01 00:15

    Try to add Space widget after adding view like this:

    layout.addView(view)
    val space = Space(context)
    space.minimumHeight = spaceInterval
    layout.addView(space)
    
    0 讨论(0)
  • 2020-12-01 00:17

    Using padding in the layout of Child View.

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

    Since API Level 14 you can just add a (transparent) divider drawable:

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

    and it will handle the rest for you!

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

    If you use ActionBarSherlock, you can use 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>
    
    0 讨论(0)
  • 2020-12-01 00:24

    You just need to wrap items with linear layouts which have layout_weight. To have items horizontally separated, use this

    <LinearLayout
        ...
        ...
      <LinearLayout
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:gravity="center">
    
          // your item
    
      </LinearLayout>
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题