単一のアイテム「プロトタイプ」を別のxmlファイルで定義し、そのファイルからアイテムをコードで動的に膨張させ、線形レイアウトに挿入することができます。
次に、親 LinearLayout ではなく、実際のアイテムで間隔を定義し (android:layout_marginTop
たとえば)、その間隔は、アイテムを膨張させるときにすべてのアイテムに適用されます。
編集:
コンテナ.xml:
<LinearLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Your items will be added here -->
</LinearLayout>
item.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is my child" />
</LinearLayout>
MyActivity.java:
// Put this in a suitable place in your Java code, perhaps
// in "onCreate" or "onResume" depending on where and how
// you initialize your view. You can, of course inflate
// any number of instances of the item and add them to
// your parent LinearLayout.
LayoutInflater inflater = LayoutInflater.from(context);
View item = inflater.inflate(R.layout.item, null, false);
LinearLayout container = findViewById(R.id.parent);
container.addView(view);
私はコードのテストに力を入れていませんが、そのまま「動作するはず」です:-)