AbsoluteLayout绝对布局已经被弃用,可是相关API依然有效,其又被称为坐标布局,在iOS开发支持Autolayout以前,全部的布局模式均可以理解为绝对布局。可是iPhone设备的屏幕尺寸有限,使用绝对不觉并不会出现太多难以解决的问题,可是对于Android设备就不一样了,Android设备的屏幕尺寸和分辨率都无规范,使用坐标绝对布局的缺陷就十分明显。java
AbsoluteLayout直接经过定位其内部视图的位置坐标点和尺寸来进行布局,后添加的视图优先级更高,若是坐标有重合,会覆盖先添加的视图,示例代码以下:ide
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AbsoluteLayout absoluteLayout = new AbsoluteLayout(this); absoluteLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); setContentView(absoluteLayout); //添加4个TextView TextView textView1 = new TextView(this); textView1.setText("第1个textView"); //须要注意 这里的LayoutParams()构造方法中的参数 前两个参数为视图的宽和高 后两个为x与y位置坐标点 textView1.setLayoutParams(new AbsoluteLayout.LayoutParams(800,300,10,10)); textView1.setBackgroundColor(Color.RED); absoluteLayout.addView(textView1); TextView textView2 = new TextView(this); textView2.setText("第2个textView"); textView2.setLayoutParams(new AbsoluteLayout.LayoutParams(800,300,100,200)); textView2.setBackgroundColor(Color.YELLOW); absoluteLayout.addView(textView2); TextView textView3 = new TextView(this); textView3.setText("第3个textView"); textView3.setLayoutParams(new AbsoluteLayout.LayoutParams(800,300,200,400)); textView3.setBackgroundColor(Color.BLUE); absoluteLayout.addView(textView3); TextView textView4 = new TextView(this); textView4.setText("第4个textView"); textView4.setLayoutParams(new AbsoluteLayout.LayoutParams(800,300,300,600)); textView4.setBackgroundColor(Color.GREEN); absoluteLayout.addView(textView4); }
布局效果以下图:布局
其实布局容器中子视图的布局参数主要有定义在各个布局容器类的内部类LayoutParams来设置。须要注意,在不一样分辨率的屏幕上,使用AbsoluteLayout布局效果可能会难于把控。this
专一技术,热爱生活,交流技术,也作朋友。spa
——珲少 QQ群:435043639code