
android toolbar中的布局怎么设置
1个回答
展开全部
在线性布局LinearLayout里加入view比较简单,因为属性比较少,布局简单
示例,加入一个TextView
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
TextView tv = new TextView(this);
tv.setText("hello,world");
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.addView(tv,lp);
在相对布局中RelativeLayout中加入view,属性较多
示例,加入TextView和Button,让TextView居中,并且设置Button在TextView的下方
RelativeLayout layout;
TextView tv = new TextView(this);
tv.setText("hello,world");
Button btn = new Button(this);
btn.setText("button");
tv.setId(0x011);
btn.setId(0x012);
LayoutParams tvLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutParams btnLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//添加布局规则,居中于父类
tvLp.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
//添加布局规则,在tv的下方
btnLp.addRule(RelativeLayout.BELOW, tv.getId());
layout.addView(tv,tvLp);
layout.addView(btn,btnLp);
public void addRule(int verb, int anchor) 方法就是给view设定布局规则,verb是规则属性,就是xml文件中的各种属性值,anchor是依靠的view的id或者比如上面的RelativeLayout.CENTER_IN_PARENT的时候就是设置true或false
示例,加入一个TextView
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
TextView tv = new TextView(this);
tv.setText("hello,world");
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.addView(tv,lp);
在相对布局中RelativeLayout中加入view,属性较多
示例,加入TextView和Button,让TextView居中,并且设置Button在TextView的下方
RelativeLayout layout;
TextView tv = new TextView(this);
tv.setText("hello,world");
Button btn = new Button(this);
btn.setText("button");
tv.setId(0x011);
btn.setId(0x012);
LayoutParams tvLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutParams btnLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//添加布局规则,居中于父类
tvLp.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
//添加布局规则,在tv的下方
btnLp.addRule(RelativeLayout.BELOW, tv.getId());
layout.addView(tv,tvLp);
layout.addView(btn,btnLp);
public void addRule(int verb, int anchor) 方法就是给view设定布局规则,verb是规则属性,就是xml文件中的各种属性值,anchor是依靠的view的id或者比如上面的RelativeLayout.CENTER_IN_PARENT的时候就是设置true或false
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询