【问题标题】:Creating a layout Multiple times in Android在 Android 中多次创建布局
【发布时间】:2015-07-02 20:17:52
【问题描述】:

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

【问题讨论】:

  • this 链接应该很有用
  • 您需要提供您一直在处理的内容,以便回答者可以查看它来帮助您解决问题,但是如何从资源中夸大视图或以编程方式创建视图?
  • 为什么不使用 ListView 或 RecyclerView,每一行都包含相关的布局?
  • 我有一个包含列表视图和两个文本视图的相对布局,我想多次动态创建整个相对布局。
  • 你可以发布你已经做过的事情吗?

标签: android android-layout android-relativelayout layoutparams


【解决方案1】:

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

或-

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

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

    时间。

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

  4. 在运行时创建一个线性布局并通过添加将这些视图添加到其中 查看

  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);

【讨论】:

  • 感谢您的想法。我现在就试试。
  • 嘿,终于用你的想法搞定了。谢谢你的想法,它很好用。
【解决方案2】:

您需要将膨胀的子视图添加到根视图,如下所示

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

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

查看此链接http://developer.android.com/training/improving-layouts/reusing-layouts.html

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多