通常我们使用XML来书写Android的图形界面,XML虽然编写方便,但是在某些情况下不如JAVA代码灵活,所以在某些环境下还是必须使用JAVA代码来编写界面。我们以下XML代码为例,编写相应的JAVA代码。
上述XML代码并不复杂。一个ImageView和两个TextView。
最终效果图是这个样子的。相应的JAVA代码如下。
我会在后面逐一解释这些代码。请忽略RelativeLayout背景和圆形的图片,本例应该是普通的ImageView.
RelativeLayout rootlayout = new RelativeLayout(this);
rootlayout.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, dip2px(120)));
rootlayout.setId(0);
ImageView imageView = new ImageView(this);
RelativeLayout.LayoutParams image_Params = new RelativeLayout.LayoutParams(
dip2px(60), dip2px(60));
image_Params.setMargins(dip2px(20), dip2px(25), 0, dip2px(10));
imageView.setLayoutParams(image_Params);
imageView.setImageResource(R.drawable.user_demo);
imageView.setId(1);
TextView text_Name = new TextView(this);
RelativeLayout.LayoutParams text_Name_Params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
text_Name_Params.addRule(RelativeLayout.RIGHT_OF, imageView.getId());
text_Name_Params.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
text_Name_Params.setMargins(dip2px(20), dip2px(10), 0, 0);
text_Name.setLayoutParams(text_Name_Params);
text_Name.setText("Mr.Zdy");
text_Name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
text_Name.setId(2);
TextView text_Email = new TextView(this);
RelativeLayout.LayoutParams text_Email_Params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
text_Email_Params.addRule(RelativeLayout.RIGHT_OF, imageView.getId());
text_Email_Params.addRule(RelativeLayout.BELOW, text_Name.getId());
text_Email_Params.setMargins(dip2px(20), dip2px(3), 0, 0);
text_Email.setLayoutParams(text_Email_Params);
text_Email.setText("zhudongya123@gmail.com");
text_Email.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
rootlayout.addView(imageView, image_Params);
rootlayout.addView(text_Name, text_Name_Params);
rootlayout.addView(text_Email, text_Email_Params);
setContentView(rootlayout);
}
public int dip2px(float dpValue) {
final float scale = this.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
首先我们定义了一个RelativeLayout,起名为rootlayout。 rootlayout.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, dip2px(120)));
ImageView imageView = new ImageView(this);
RelativeLayout.LayoutParams image_Params = new RelativeLayout.LayoutParams(
dip2px(60), dip2px(60));
image_Params.setMargins(dip2px(20), dip2px(25), 0, dip2px(10));
imageView.setLayoutParams(image_Params);
imageView.setImageResource(R.drawable.user_demo);
imageView.setId(1);
public int dip2px(float dpValue) {
final float scale = this.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
TextView text_Name = new TextView(this);
RelativeLayout.LayoutParams text_Name_Params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
text_Name_Params.addRule(RelativeLayout.RIGHT_OF, imageView.getId());
text_Name_Params.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
text_Name_Params.setMargins(dip2px(20), dip2px(10), 0, 0);
text_Name.setLayoutParams(text_Name_Params);
text_Name.setText("Mr.Zdy");
text_Name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
text_Name.setId(2);
rootlayout.addView(imageView, image_Params);
rootlayout.addView(text_Name, text_Name_Params);
rootlayout.addView(text_Email, text_Email_Params);
setContentView(rootlayout);
个人观点,仅供参考。
参考资料:
[addRule方法的使用](http://zhidao.baidu.com/link?url=R6E6Hwoea_SQFi5U_OwqZq0lqihpRkWYpOw9nZReYRc-1M4eUMzvyHjPn5FL0323gfSPssBLZLTgeKPeMrQ7W875uBxsUJbOaavpPmFCwMW)
[Android 步步为营 第5营 代码控制UI,View](http://www.cnblogs.com/vivid-stanley/archive/2012/08/22/2651399.html)
[android-getTextSize返回值是以像素(px)为单位的,setTextSize()以sp为单位](http://blog.csdn.net/lizhenmingdirk/article/details/7349386)
[Android中LayoutParams的用法](http://www.cnblogs.com/zhengbeibei/archive/2013/03/29/2989263.html)