android 常常需要动态添加布局例如:
需要后台返回的天数个数来动态添加布局,有一下几种写法:
方式1。 基于recycleView
recyclerView使用了基于mvvm和databinding的框架github地址
xml布局,通过后台返回天数的数量daySize显示多少个布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="me.tatarka.bindingcollectionadapter2.LayoutManagers"/>
<variable
name="viewCtrl"
type="com.example.example.dynamicAddLayout.DynamicViewCtrl"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity=