Android使用绝对布局AbsoluteLayout动态添加控件
程序员文章站
2022-06-22 17:16:35
layout.xml
- 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);
本文地址:https://blog.csdn.net/quantum7/article/details/111910944
上一篇: @vue/cli 4.4.6解决跨域问题
下一篇: 起床是会呼吸的痛