前言:以前曾写过一篇关于动态生成控件的文章《动态添加控件及将某XML动态加入到Activity显示》,比较浅显,对于RelativeLayout的相关布局设置方法及相对布局与线性布局的混合使用的相关内容都没有进行深入讨论。今天再次涉及到这些内容,就不再单独讨论相对布局的相关设置内容了,直接从相对布局与线性布局的混合应用开始。
相关文章:《动态添加控件及将某XML动态加入到Activity显示》
总效果:
这里动态生成十个相同的列表,这是最终效果,但凡事都是从易而难的,下面我们就从XML生成一项内容开始讲解。
一、利用XML生成一项列表
这里先利用XML生成一项列表开始,先看一项列表的效果图及对应代码:
对应的XML代码为:
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/layout_root"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#19000000"
- android:gravity="center_horizontal"
- android:paddingBottom="20dip"
- android:paddingTop="20dip"
- android:text="尝试动态生成列表"
- android:textColor="#ff0000"
- android:textSize="24sp"/>
- <ScrollView
- android:layout_width="fill_parent"
- android:layout_height="match_parent"
- android:scrollbars="vertical">
- <LinearLayout
- android:id="@+id/list_Lin"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <!--动态生成部分开始-->
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="5dip"
- android:layout_marginRight="10dip"
- android:layout_toLeftOf="@+id/image"
- android:background="#ff00ff00"
- android:orientation="horizontal"
- android:padding="5dip">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="我的第一次经历"
- android:textColor="#ff000000"
- android:textSize="20dip"/>
- </LinearLayout>
- <ImageView
- android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:clickable="true"
- android:padding="5dip"
- android:src="@drawable/plus"/>
- </RelativeLayout>
- <!--动态生成部分结束-->
- </LinearLayout>
- </ScrollView>
- </LinearLayout>