Android中怎么用纯代码编写布局?
来源:互联网 发布:精仿雨轩qq教程网源码 编辑:程序博客网 时间:2024/06/10 17:32
一、用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);
- Android中怎么用纯代码编写布局?
- android 纯代码 详细编写布局文件
- android纯代码布局
- Android纯代码写布局
- ios中纯手写代码怎么实现内容的自动布局
- ios中纯手写代码怎么实现控件的自动布局(约束)
- android 用代码编写linearlayout布局
- android 用代码编写linearlayout布局
- android使用代码编写布局
- 纯代码实现 Android 登陆布局.
- iOS编程 界面布局 纯代码 VFL编写和添加
- 用纯代码编写hello world
- 纯代码布局
- 纯代码布局控件
- 怎么高效率的编写android代码
- android代码编写布局和控件
- 【转】Android 纯代码化编码1_关于布局
- 纯java代码布局android RadioButton 遇见的问题
- 程序员面试、算法研究、编程艺术、红黑树、数据挖掘5大系列集锦
- 【百为学习板】 例程1- LED流水灯
- C# 流概括
- 初用hibernate和struts2结合时的总结
- 在Android 中使用KSOAP2调用WebService
- Android中怎么用纯代码编写布局?
- matlab和数据库连接
- 【Android】使用Chronometer组件实现一个简单的计时器
- linux 文件系统
- 黑马程序员--常用类总结
- Ubuntu上开发Android,手机调试配置
- bitmap保存
- matlab命令
- C#里与Java里的静态类与类成员