Android中怎么用纯代码编写布局

如题所述

一、用RelativeLayout进行纯代码布局的理论基础
1、RelativeLayout,顾名思义,就是以“相对”位置/对齐 为基础的布局方式。
2、android.widget.RelativeLayout 有个继承自android.view.ViewGroup.LayoutParams 的内嵌类 LayoutParams,使用这个类的实例
调用RelativeLayout.addView 就可以实现“相对布局”。 android.widget.RelativeLayout.LayoutParams 有一个构造函数:
RelativeLayout.LayoutParams(int w, int h),参数指定了子 View 的宽度和高度,这一点和其父类是一样的。而实现相对布局的关
键在它的 两个 addRule 方法上。anchor 参数指定可以是View 的 id(“相对于谁”)、RelativeLayout.TRUE(启用某种对齐方式) 或者
是-1(应用于某些不需要 anchor 的 verb);AddRule 方法的 verb 参数指定相对的“动作”(以下常量均定义于
android.widget.RelativeLayout中,为了简便不给出其全名):
  3、ALIGN_BOTTOM、ALIGN_LEFT、 ALIGN_RIGHT、 ALIGN_TOP: 本 View 的 底边/左边/右边/顶边 和 anchor 指定的 View 的
底边/左边/右边/顶边 对齐。
ALIGN_WITH_PARENT_BOTTOM 、ALIGN_WITH_PARENT_LEFT 、 ALIGN_WITH_PARENT_RIGHT 、
ALIGN_WITH_PARENT_TOP : 和上面一组常量类似,只不过不需要再指定 anchor, 其 anchor 自动为 Parent View。
CENTER_HORIZONTAL、CENTER_IN_PARENT 、CENTER_VERTICAL : 如果 anchor 为 TRUE,在 Parent 中 水平居中/水平
和垂直均居中/垂直居中。
POSITION_ABOVE 、POSITION_BELOW 、 POSITION_TO_LEFT 、POSITION_TO_RIGHT : 本 View 位于 anchor 指定的 View
的上边/下边/左边/右边。
二、案例
1、布局文件如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" >
<com.baidu.mapapi.map.MapView
android:id="@+id/baidu_map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" >
</com.baidu.mapapi.map.MapView>
<RelativeLayout
android:id="@+id/anquan_map_l1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp" >
<ImageButton
android:id="@+id/but_of_lukuang"
android:layout_width="38.0dip"
android:layout_height="38.0dip"
android:background="@drawable/main_map_button_bg"
android:src="@drawable/maptraffic_icon_off" />
<ImageButton
android:id="@+id/btn_of_bobao"
android:layout_width="38.0dip"
android:layout_height="38.0dip"
android:layout_below="@id/but_of_lukuang"
android:layout_marginTop="5dp"
android:visibility="gone"
android:background="@drawable/main_map_button_bg"
android:src="@drawable/netfriend_bobao_n" />
<ImageButton
android:id="@+id/btn_of_layer"
android:layout_width="38.0dip"
android:layout_height="38.0dip"
android:layout_below="@+id/btn_of_bobao"
android:layout_marginTop="5dp"
android:background="@drawable/main_map_button_bg"
android:src="@drawable/main_map_icon_layer" />
</RelativeLayout>
</RelativeLayout>
2、代码如下
//得到
mapButtonRL = (RelativeLayout) findViewById(R.id.anquan_map_l1);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.BELOW, R.id.btn_of_layer);
showModeButton = new Button(this);
showModeButton.setText("全部显示");
showModeButton.setId(SHOW_MODE);
showModeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
mapButtonRL.addView(showModeButton, lp1);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.BELOW, SHOW_MODE);
positionButton = new Button(this);
positionButton.setText("位置");
positionButton.setId(POSITION);
positionButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
mapButtonRL.addView(positionButton, lp2);
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-04-08

很麻烦,总之。

我以相对布局为例子说明下

        btn1 = new Button(this);   
        btn1.setText("我是按钮");   
        btn1.setId(ID_BTN1);   
          
        RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);   
        lp1.addRule(RelativeLayout.ALIGN_WITH_PARENT_TOP);   
        lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);   
        // btn1 位于父 View 的顶部,在父 View 中水平居中   
        rl = new RelativeLayout(this);
        rl.addView(btn1, lp1 );
        
        //然后在在onCreate()方法里用
        setContentView(rl);

本回答被网友采纳
第2个回答  2018-08-25
//定义一个线性布局LindearLayout  tp = new LindearLayout  (this);//定义一个布局参数类(用于定义Button在线性布局中的参数)LayoutParams ltp = new LayoutParams(LayoutParams.WARP_CONTENT,LayoutParams.WARP_CONTENT);Button lbt = new Button(this);tp.addView(lbt,ltp);//将Button加入到线性布局中。 //但是不推荐代码里实现,因为android是MVC开发模式,数据操作,UI,代码等实现都是分开写的,这样写的好处你多编点代码就会体会到了。虽然靠纯代码甚至是在一个activity中都可能完成一个app的编写,但是这样就完全没了可维护性了

第3个回答  2017-06-14
用view画出来,先获取屏幕的长和宽,然后按一定的比例进行布局。
第4个回答  2017-06-19
用LayoutParams
相似回答