view中如何用代码动态添加图像按钮

yuli 2011-01-06 09:19:25
我是代码大概如下

需要在view中添加两个按钮,要能够自定义位置

public class myapp extends Activity {
/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
View view = new MyView(this);

setContentView(view);



}



class MyView extends View {




public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub



...............................

}

}

}

谢谢
...全文
718 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
heisenberg07 2011-10-11
  • 打赏
  • 举报
回复
嗯,楼上的方法可以用~~
wulz 2011-09-02
  • 打赏
  • 举报
回复
这个问题太纠结了,我也是想将按钮添加到自定义的view中的,但是一直没有实现,都一天多了,还没有得到解决呢,求高手告诉我方法!
yuli 2011-01-08
  • 打赏
  • 举报
回复
我的程序是
ImageView Myview = new ImageView(this);

setContentView(view);

希望按钮能够加到ImageView 中,并响应点击消息
pzz04120202 2011-01-07
  • 打赏
  • 举报
回复
动态生成界面的方法很简单
首先要却这个界面的布局,是AbsoluteLayout,RelativeLayout还是其他,然后就可以再里面添加控件了:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//确定界面的布局
AbsoluteLayout abslayout=new AbsoluteLayout (this);
setContentView(abslayout);
//创建一个button按钮
Button btn1 = new Button(this);
btn1.setText("this is a button");
btn1.setId(1);
//确定这个控件的大小和位置
AbsoluteLayout.LayoutParams lp1 =
new AbsoluteLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0,100);
abslayout.addView(btn1, lp1 );

}
一个界面可以布置一个布局,可以多个布局一起设计:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//设置界面的布局
RelativeLayout relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);

//添加一个AbsoluteLayout子布局,并给这个布局添加一个button
AbsoluteLayout abslayout=new AbsoluteLayout (this);
abslayout.setId(11);
Button btn1 = new Button(this);
btn1.setText("this is a abslayout button");
btn1.setId(1);
AbsoluteLayout.LayoutParams lp0 = new AbsoluteLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,100,0);
abslayout.addView(btn1, lp0 );
//将这个子布局添加到主布局中
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
relativeLayout.addView(abslayout ,lp1);

//再添加一个子布局
RelativeLayout relativeLayout1 = new RelativeLayout(this);
Button btn2 = new Button(this);
btn2.setText("this is a relativeLayout1 button");
btn2.setId(2);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp2.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
relativeLayout1.addView(btn2 ,lp2);

//将这个布局添加到主布局中
RelativeLayout.LayoutParams lp11 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp11.addRule(RelativeLayout.BELOW ,11);
relativeLayout.addView(relativeLayout1 ,lp11);
}
pzz04120202 2011-01-07
  • 打赏
  • 举报
回复
可以看一下下面的文章
http://blog.chinaunix.net/u1/57427/showart_1987303.html
yuhuaijun 2011-01-07
  • 打赏
  • 举报
回复

AbsoluteLayout viewlayout = new AbsoluteLayout(this);

/**
* width: 50
* height: 50
* x: 0
* y: 0
*/
AbsoluteLayout.LayoutParams absLayout= new AbsoluteLayout.LayoutParams(50, 50, 0, 0);
Button button = new Button(this);
viewlayout.addView(button, absLayout);

view.addView(viewlayout);


80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧