Collapsing margins in Android layouts Collapsing margins in Android layouts android android

Collapsing margins in Android layouts


What I typically do to fix this myself, is to simply cut the View's (i.e. your TextView) margin in half, and add that same number as padding to the containing ViewGroup (i.e. your LinearLayout). This way you will end up with even spacing around all items. For example:

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="5dip"    >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="5dip"        android:text="I'm a TextView!"        />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="5dip"        android:text="I'm a TextView!"        />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="5dip"        android:text="I'm a TextView!"        /></LinearLayout>