通过代码实现RelativeLayout的布局

来源:互联网 发布:ansys15.0软件 编辑:程序博客网 时间:2024/06/06 01:24

一:布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:id="@+id/rl_product_parent"     android:background="@drawable/login_bg">    <RelativeLayout         android:id="@+id/rl_product_main"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_marginTop="51dp"        >           <ImageView         android:id="@+id/iv_jiuyingbao"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginRight="5dp"        android:src="@drawable/jiuyingbao"/>    <ImageView         android:id="@+id/iv_zhinengcun"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toRightOf="@id/iv_jiuyingbao"        android:layout_alignTop="@id/iv_jiuyingbao"        android:src="@drawable/zhinengcun"/>    <ImageView         android:id="@+id/iv_diydai"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignRight="@id/iv_zhinengcun"        android:layout_toRightOf="@id/iv_jiuyingbao"        android:layout_alignBottom="@id/iv_jiuyingbao"        android:src="@drawable/diydai"/>    <ImageView         android:id="@+id/iv_wodezhanghu"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/iv_jiuyingbao"        android:layout_alignLeft="@id/iv_jiuyingbao"        android:layout_alignRight="@id/iv_diydai"        android:layout_marginTop="5dp"        android:src="@drawable/wodezhanghu"/>    <ImageView         android:id="@+id/iv_dangmianfu"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/iv_wodezhanghu"        android:layout_alignLeft="@id/iv_wodezhanghu"        android:layout_marginTop="5dp"        android:src="@drawable/dangminafu"/>     <ImageView          android:id="@+id/iv_zhuanzhang"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignTop="@id/iv_dangmianfu"         android:layout_alignRight="@+id/iv_wodezhanghu"         android:src="@drawable/zhuanzhang"/>      </RelativeLayout></RelativeLayout>

二:java代码

rl_product_main = (RelativeLayout) main.findViewById(R.id.rl_product_main);RelativeLayout.LayoutParams rparams = new RelativeLayout.LayoutParams(rl_product_main.getLayoutParams());//说明当前相对布局距离父控件的顶部100像素,注意是像素rparams.topMargin = 100;//宽高是包裹内容rparams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;rparams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;/** * 一个参数的方法:addRule (int verb) * 给当前相对布局添加一个布局规则,即一些属性等,但是这个布局规则不能涉及到其他兄弟姐妹;或者这个布局规则是一个布尔类型的,设置了即为true,不设置即为false * 两个参数的方法:addRule (int verb, int anchor) * 给当前相对布局添加一个布局规则,这个布局规则可以涉及到其它兄弟姐妹元素,此时第二个参数即为所涉及到的兄弟姐妹的id;或者这个布局规则是一个布尔类型的,此时 * 第二个参数即为RelativeLayout.TRUE代表为true或者0代表为false */rparams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);rl_product_main.setLayoutParams(rparams);




0 0