动态创建View的方法,包括Button等等

创建 myView:

UIVIew *myView = [[UIView alloc] initWithFrame:CGRectMaker(0,0,100,100)];
[anyView  addSubview:myView];
[myView release];

销毁myView;
[myView removeFromSuperview];



在view中创建button:

UIButton *btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
btn.frame = CGRectMake(60,60, 60, 60);
[self.view addSubview:btn];

在view中销 毁button:
[btn removeFromSuperview];
[btn release];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用绝对布局(AbsoluteLayout)来动态创建并层叠视图(View)在Android应用中。下面是一个示例代码,演示如何使用绝对布局动态创建和层叠视图: ```java // 创建绝对布局 AbsoluteLayout absoluteLayout = new AbsoluteLayout(context); absoluteLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); // 创建第一个视图 TextView textView1 = new TextView(context); textView1.setText("View 1"); AbsoluteLayout.LayoutParams params1 = new AbsoluteLayout.LayoutParams( AbsoluteLayout.LayoutParams.WRAP_CONTENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT, 100, 100); // 设置视图的位置 textView1.setLayoutParams(params1); absoluteLayout.addView(textView1); // 创建第二个视图 TextView textView2 = new TextView(context); textView2.setText("View 2"); AbsoluteLayout.LayoutParams params2 = new AbsoluteLayout.LayoutParams( AbsoluteLayout.LayoutParams.WRAP_CONTENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT, 200, 200); // 设置视图的位置 textView2.setLayoutParams(params2); absoluteLayout.addView(textView2); // 将绝对布局添加到父容器中 ViewGroup parentView = findViewById(R.id.parent_view); // 替换为你的父容器视图ID parentView.addView(absoluteLayout); ``` 在这个示例中,我们首先创建了一个绝对布局对象 `absoluteLayout`,并设置了其布局参数为匹配父容器大小。然后,我们创建了两个文本视图 `textView1` 和 `textView2`,并分别设置了它们在绝对布局中的位置。最后,我们将这两个视图添加到了绝对布局中,并将绝对布局添加到父容器中。 注意:绝对布局(AbsoluteLayout)在较新的Android版本中已被弃用,推荐使用其他布局方式,比如相对布局(RelativeLayout)或线性布局(LinearLayout)来实现动态创建和层叠视图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值