第三次_布局

来源:互联网 发布:他趣是什么软件 编辑:程序博客网 时间:2024/06/07 05:34
编码规范:
1.变量的命名规范:驼峰命名,见名知意
2.变量的修饰符
3.写注释   变量和方法
4.写一个方法的时候遵循基本规则:getXX,setXX


五大布局:(尽量减小布局的嵌套)
1.线性布局
android:orientation="horizontal"水平
android:orientation="vertical"垂直
android:layout_margin  外边距
android:padding        内边距
设置宽高:
match_parent 或 fill_parent :  匹配父容器,表示的是和父容器的宽或高一样,也就是尽可能大
wrap_content  :自适应 表示控件的宽和高和自身要显示的内容有关  尽可能小
android:layout_weight="1"
如果我们的控件要按照百分比布局就要使用layout_weight属性:
layout_weight的规则:设置了该属性的控件的大小按比例进行分配大小(剩余的空间按照比例分配)


使用layout_weight权重,控件的大小实际是计算两次:


android:orientation="horizontal"水平为例:


button1 权重为1         button2 权重为2


假设屏幕的宽度为L
情况1:android:layout_width="wrap_content"或"0dp"   尽可能的小
计算过程:
第一次计算:
button1  宽度是0         button2  宽度是0
第二次计算:  剩下的宽度L
button1  宽度是L/3       button2  宽度是L*2/3
那么最后的结果:
button1  宽度是L/3       button2  宽度是L*2/3




情况2:android:layout_width="match_parent"或"fill_parent"   尽可能的大
计算过程:
第一次计算:
button1  宽度是L         button2  宽度是L
第二次计算:  剩下的宽度-L
button1  宽度是-L/3       button2  宽度是-L*2/3
那么最后的结果:
button1  宽度是L*2/3       button2  宽度是L/3


所以:为了要我们的控件的大小严格的按照我们想要设置的百分比显示,我们要将对应的宽或高的值改成0dp最好不要用wrap_content
2.相对布局:最好更多使用相对布局
相对于某一个视图的位置,先选择一个参照
3.帧布局
4.表格布局
5.绝对布局


动态布局:通过代码实现
public LinearLayout initView() {
// 根布局
LinearLayout screen = new LinearLayout(this);
// 设置布局参数
screen.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
// 设置背景颜色
screen.setBackgroundColor(Color.WHITE);
// 设置内边距
screen.setPadding(30, 30, 30, 30);//里面的值是px 不是 dp


// 设置布局的样式(水平还是垂直)
screen.setOrientation(LinearLayout.HORIZONTAL);


// 准备添加子View
Button bt1 = new Button(this);
bt1.setTextColor(Color.RED);
bt1.setText("button1");




Button bt2 = new Button(this);
bt2.setTextColor(Color.BLACK);
bt2.setText("button2");


screen.addView(bt2);


return screen;
}
0 0
原创粉丝点击