Android 动态布局该如何实现!!??
3个回答
展开全部
你的问题应该是要动态生成空间吧。
下面的答案可以参考下。
首先要却这个界面的布局,是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);
}
下面的答案可以参考下。
首先要却这个界面的布局,是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);
}
追问
那该如何确定一个控件或是一个View的具体位置呢,比如我点击了屏幕的某个位置,我需要在这个位置上显示一个View或控件,那该如何实现!??谢谢!!!
展开全部
华夏博大3G学院,都反映挺好的,那里的老师有这方面的开发经验。
具备着线上的真实应用,就业了之后再付 款。
具备着线上的真实应用,就业了之后再付 款。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用java语言 像 linearlayout linear=new linearlayout(this)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询