layout.xml
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/absolute_layout" > </AbsoluteLayout> Activity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.eframe); absLayout = (AbsoluteLayout)findViewById(R.id.absolute_layout); absLayout.setBackgroundColor(Color.RED); }
新建控件
tv = new TextView(this.getApplicationContext()); tv.setBackgroundColor(Color.BLUE); tv.setText("泰山Office");
添加控件
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(width, height, x, y); absLayout.addView(v, params);
需要强调的是,如果之前给控件设置宽高,添加时获取,是错误的。以下代码无效:
v.setWidth( 300); v.setHeight(100); # 无法显示出来。因为得到的宽高是0。 AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(v.getWidth(), v.getHeight(), x, y); absLayout.addView(v, params);