在 Android 中多次创建布局

Creating a layout Multiple times in Android

如何在Android中多次创建整个布局(Relative/Linear)?我希望在水平滚动视图中多次创建相同的布局。

您可以使用 RecyclerView 进行水平滚动-

或-

  1. findViewById 在 java 代码中引用水平滚动视图。

  2. 为要显示多个的视图创建另一个xml

    时间。

  3. 将该视图放大 getlayoutinflator。在中创建一个循环 看法。

  4. 在运行时创建一个线性布局并通过 add 添加那些视图 查看

  5. 并将线性布局添加到水平滚动视图。通过 addview()

想一想修改下面的代码

scrollview = findViewByID(scrollview);
LinearLayout ll = new LinearLayout(this);
for(your loop){
View v= getLayoutInflator().inflate(R.layout.xml);
ll.addView(v);
}
scrollview.addView(ll);

要么你需要像下面那样将膨胀的子视图添加到根视图

RelativeLayout rootView = (RelativeLayout)findViewById(R.id.rootView);
View child = getLayoutInflater().inflate(R.layout.child, null);
rootView.addView(child);

或者您可以定义并包含该布局在其他内部多次。

勾选这个linkhttp://developer.android.com/training/improving-layouts/reusing-layouts.html

像这样包含您的可重用布局

<include
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   layout="@layout/reusabelLayout" />