android:below用java代码怎么实现
2个回答
展开全部
android:below表示在某个控件下面,用代码首先是设置一个布局参数,如下代码:
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
RelativeLayout rl=new RelativeLayout(this);
//创建TextView1
TextView tv1=new TextView(this);
tv1.setText("111111111111111111111111111");
tv1.setTextSize(20);
//设定 tv1 的 id ,后面规则要用到
tv1.setId(1);
//将tv1加入布局
rl.addView(tv1);
//创建TextView2
TextView tv2=new TextView(this);
tv2.setText("2222222222222222222222222222");
tv2.setTextSize(20);
//创建RelativeLayout.LayoutParams
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//添加规则 1为tv1的id
params.addRule(RelativeLayout.BELOW,1);
//将tv2加入布局 传入参数params
rl.addView(tv2,params);
setContentView(rl);
}
展开全部
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
RelativeLayout rl=new RelativeLayout(this);
//创建TextView1
TextView tv1=new TextView(this);
tv1.setText("111111111111111111111111111");
tv1.setTextSize(20);
//设定 tv1 的 id ,后面规则要用到
tv1.setId(1);
//将tv1加入布局
rl.addView(tv1);
//创建TextView2
TextView tv2=new TextView(this);
tv2.setText("2222222222222222222222222222");
tv2.setTextSize(20);
//创建RelativeLayout.LayoutParams
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//添加规则 1为tv1的id
params.addRule(RelativeLayout.BELOW,1);
//将tv2加入布局 传入参数params
rl.addView(tv2,params);
setContentView(rl);
}
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
RelativeLayout rl=new RelativeLayout(this);
//创建TextView1
TextView tv1=new TextView(this);
tv1.setText("111111111111111111111111111");
tv1.setTextSize(20);
//设定 tv1 的 id ,后面规则要用到
tv1.setId(1);
//将tv1加入布局
rl.addView(tv1);
//创建TextView2
TextView tv2=new TextView(this);
tv2.setText("2222222222222222222222222222");
tv2.setTextSize(20);
//创建RelativeLayout.LayoutParams
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//添加规则 1为tv1的id
params.addRule(RelativeLayout.BELOW,1);
//将tv2加入布局 传入参数params
rl.addView(tv2,params);
setContentView(rl);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询