本文实例讲述了Android动态添加设置布局与控件的方法。分享给大家供大家参考,具体如下:
有时候我们会在代码端,动态的设置,添加布局和控件。下面我们就看来看一下如何处理,直接上代码,代码里面的注解很清楚了。
布局文件:fragment_hot.xml
说明:这个部局,我用的是scrollView做为基础布局,主要是为了实现一个滚动。这里不多说,这个你可以使用任何布局都可以,这里的id我是提前定义的。
这里面的现在有的布局是我为了看到我在代码端,动态添加的代码,是否可以追加到现有布局的后面而加上,这里大家可以自己设置
android:id="@id/id_stickynavlayout_innerscrollview"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/line_fragment_hot_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="fill_parent"
android:layout_height="80dp"
android:orientation="horizontal">
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/desktop_device"/>
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="燕郊孔雀城"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="燕郊 燕郊"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3万抵6万"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6000元/m"/>
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:gravity="right">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剩2天"
android:textColor="#F97F49"
android:textSize="12dp"
android:textStyle="bold"
android:layout_marginTop="2dp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background&