android编程之代码布局

前言:

习惯了用xml来写布局的,再用代码来写布局,恐怕会很类。但毕竟有时候,我们还是需要用到代码来写布局。
代码布局与xml布局有很多相似点,在大多数方法上都可以直接用,只有个别方法的写法不太一样,接下来,我将分几篇来介绍常用的几种控件。

以android工程自带的main.xml为例,看看代码是如何实现的

一、设置当前布局
关于这个问题只针对初级水平的人员,其他人可略过。

我们知道,在初建一个activity的时候,程序会帮我们建好:

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

setContentView(R.layout.main);就是设置当前布局的样式。
我们用代码写布局的话,这里就要传入一个View对象。实际上它就是线性布局,相对布局,文本框等的对象。比如我们创建了一个线性布局,那么我们这里就将它的传进去,例如:

		LinearLayout mLinearLayout = new LinearLayout(this);
		setContentView(mLinearLayout);

二、线性布局线性布局是我们经常使用的,我们先从它入手来熟悉代码布局。

		// 创建LinearLayout对象
		LinearLayout mLinearLayout = new LinearLayout(this);

		// 建立布局样式宽和高,对应xml布局中:
		// android:layout_width="fill_parent"
		// android:layout_height="fill_parent"
		mLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

		// 设置方向,对应xml布局中:
		// android:orientation="vertical"
		mLinearLayout.setOrientation(LinearLayout.VERTICAL);

LayoutParams是有很多种的,如果你不熟悉的话,最好在前面加上LinearLayout,以便于你识别。

这样,我们就最简单的建立一个外层布局,接下来,我们将实现TextView。

三、TextView

		// 创建TextView对象
		TextView mTextView = new TextView(this);
		// 设置文字
		mTextView.setText("hello world");

		// 为其建立布局样式,对应xml布局中:
		// android:layout_width="fill_parent"
		// android:layout_height="wrap_content"
		LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

		// 在父类布局中添加它,及布局样式
		mLinearLayout.addView(mTextView, mLayoutParams);

这里为其设置的布局样式与父类不同,你可以理解为,它设置的布局样式并不是为自己设置的,而是要告诉父类,我要摆放在什么位置上。理解这一点很关键,因为它是设置组件位置的关键!

它还有很多其他的参数设置:
mTextView.setTextColor(-1);//字体颜色
mTextView.setTextSize(16);//字体大小


最后:如果代码没有错误的话,运行起来和运行xml是一样的。

下面是完整版代码:

public class TestLayout extends BaseActivity {

	@Override
	protected void initRecourse() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

	@Override
	protected ViewGroup initView() {
		// 创建LinearLayout对象
		LinearLayout mLinearLayout = new LinearLayout(this);

		// 建立布局样式宽和高,对应xml布局中:
		// android:layout_width="fill_parent"
		// android:layout_height="fill_parent"
		mLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

		// 设置方向,对应xml布局中:
		// android:orientation="vertical"
		mLinearLayout.setOrientation(LinearLayout.VERTICAL);

		// 创建TextView对象
		TextView mTextView = new TextView(this);
		// 设置文字
		mTextView.setText("hello world");

		// 为其建立布局样式,对应xml布局中:
		// android:layout_width="fill_parent"
		// android:layout_height="wrap_content"
		LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

		// 在父类布局中添加它,及布局样式
		mLinearLayout.addView(mTextView, mLayoutParams);

		return mLinearLayout;
	}
}


附:重写的activity
这里的重写activity是为了更好为我们编写代码布局服务,在以后的代码范例中,都会采用下面继承该类方式。

public abstract class BaseActivity extends Activity {

	public Handler handler;

	/** 初始化数据 */
	protected abstract void initData();

	/** 初始化资源 */
	protected abstract void initRecourse();

	/** 初始化界面 */
	protected abstract View initView();

	/** 处理handler回传的信息 */
	public void dispatchMessage(Message msg) {
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		initData();

		initRecourse();

		setContentView(initView());

		handler = new Handler() {
			public void dispatchMessage(Message msg) {
				BaseActivity.this.dispatchMessage(msg);
			}
		};
	}
}

注意:这里的资源加载一般是从asset中加载进来的

————————————————————————————


一、Caused by:java.lang.IllegalStateException: The specified child already has a parent.
造成这个原因,是组件在父类中重复加载了相同的组件

例如:

  1. TextView mTextView = new TextView(this);  
  2. mTextView.setText("hello world");  
  3.   
  4. LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(  
  5.         LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
  6.   
  7. // 第一次添加  
  8. mLinearLayout.addView(mTextView, mLayoutParams);  
  9. // 第二次添加  
  10. mLinearLayout.addView(mTextView, mLayoutParams);  

这样的话,我们重复添加了两次mTextView。这个是不允许的,在父类布局中,只能有唯一的对象,不能重复。

如果你想建立两个相同的组件,还是费力一下,再创建一个对象吧。o(╯□╰)o

——————————————————————————————————————



上一篇,我们已经介绍了一点代码布局,接下来,我们接着介绍

一、常用组件设置
Button,ImageView等组件基本上和xml中所定义的基本一样,就不再详细介绍了。

给出一些常用的设置方式,供大家参考:
setVisibility(View.VISIBLE)  //是否可见
requestFocus()  //获得焦点
setGravity(Gravity.CENTER_VERTICAL)  //内部位置对齐方式
setPadding(10, 5, 5, 5);  //在内部距离各边距离
setId(1);  //设置ID标示

对于xml中,android:layout_marginTop的设置,组件是没有这个设置方法的。其实从layout_marginTop字面意义也可以看出,它是针对父类布局设置位置的。
这里给出个关于设置layout_marginTop的例子:

  1. mLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT,  
  2.         LayoutParams.WRAP_CONTENT);  
  3. mLayoutParams.addRule(RelativeLayout.RIGHT_OF, ID_IMAGE_HEAD);  
  4. mLayoutParams.topMargin = 5;  
  5. mLinearLayout.addView(mTextView, mLayoutParams);  
如果对于上一篇关于TextView组件位置的设置有印象的话,这里就不难理解了,它就是告诉父类对象,当前组件针对父类布局要放到什么位置。

关于EditText这个组件,如何设置最大输入字数,这里只给个例子:
  1. mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(8) });  
我这里设置最多可输入的字数为8个。

二、相对布局
除了经常使用的线性布局外,还常会用到相对布局,尤其是它的相对位置设计非常适合机型适配要求。

在写相对布局的xml时候,我们几乎要位每个最近设置ID标示,好告诉其他组件相对位置关系。在代码布局中也同样如此,需要为它们独立设置ID标示,而且不要重复,否则位置关系就会错误。

下面,我们给出一段例子,方便大家理解:
  1. // 创建RelativeLayout对象  
  2. RelativeLayout mRelativeLayout = new RelativeLayout(this);  
  3. mRelativeLayout.setLayoutParams(new LinearLayout.LayoutParams(  
  4.         LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
  5.   
  6. // TextView组件:白色30号字  
  7. TextView mTextView = new TextView(this);  
  8. mTextView.setTextSize(30);  
  9. mTextView.setTextColor(-1);  
  10. mTextView.setText("hello world");  
  11. // 为其设置唯一的id标示  
  12. mTextView.setId(1);  
  13.   
  14. // 为其设置位置关系  
  15. RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(  
  16.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  17. // 距离顶部20  
  18. mLayoutParams.topMargin = 20;  
  19. // 距离左边20  
  20. mLayoutParams.leftMargin = 20;  
  21. // 添加组件  
  22. mRelativeLayout.addView(mTextView, mLayoutParams);  
  23.   
  24. // TextView组件:红色20号字  
  25. mTextView = new TextView(this);  
  26. mTextView.setTextSize(20);  
  27. mTextView.setTextColor(0xffff0000);  
  28. mTextView.setText("hello world");  
  29. // 为其设置唯一的id标示  
  30. mTextView.setId(2);  
  31.   
  32. // 为其设置位置关系  
  33. mLayoutParams = new RelativeLayout.LayoutParams(  
  34.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  35. // 距离顶部20  
  36. mLayoutParams.topMargin = 20;  
  37. // 距离左边20  
  38. mLayoutParams.leftMargin = 20;  
  39. // 设置它在白色字的下面  
  40. mLayoutParams.addRule(RelativeLayout.BELOW, 1);  
  41. // 水平居中显示  
  42. mLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);  
  43.   
  44. // 添加组件  
  45. mRelativeLayout.addView(mTextView, mLayoutParams);  

效果图:

——————————————————————————————————————————————————

前两篇介绍了一般常用的布局组件写法,这一次,再来看看其他组件的

一、listview

listview也和大多数组件一样,一般会在这里难住的,就是自定义布局样式的时候。

  1. // 创建LinearLayout对象  
  2. LinearLayout mRelativeLayout = new LinearLayout(this);  
  3. mRelativeLayout.setLayoutParams(new LinearLayout.LayoutParams(  
  4.         LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
  5.   
  6. //  创建ListView对象  
  7. ListView mListView = new ListView(this);  
  8. mListView.setAdapter(new ListAdapter(this, mVector));  
  9.   
  10. // 添加组件  
  11. mRelativeLayout.addView(mListView, new LayoutParams(  
  12.         LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  

其中,ListAdapter为继承BaseAdapter的自定义类。

mVector是我们自定义的数据,里面包含了两个名字:小明,小刚。

接下来,我们看看重点的ListAdapter是如何自定义布局的

  1. public class ListAdapter extends BaseAdapter {  
  2.     private Vector<String> mVector;  
  3.   
  4.     private Context mContext;  
  5.   
  6.     public ListAdapter(Context mContext, Vector<String> mVector) {  
  7.         this.mContext = mContext;  
  8.         this.mVector = mVector;  
  9.     }  
  10.   
  11.     public Object getItem(int position) {  
  12.         return null;  
  13.     }  
  14.   
  15.     public long getItemId(int position) {  
  16.         return 0;  
  17.     }  
  18.   
  19.     public int getCount() {  
  20.         return mVector.size();  
  21.     }  
  22.   
  23.     class ViewHolder {  
  24.         ImageView head;  
  25.         TextView name;  
  26.     }  
  27.   
  28.     public View getView(int position, View convertView, ViewGroup parent) {  
  29.         ViewHolder holder = null;  
  30.         RelativeLayout rl = null;  
  31.         if (convertView == null) {  
  32.             rl = new RelativeLayout(mContext);  
  33.             rl.setBackgroundDrawable(bitmapDrawable_listitembg);  
  34.   
  35.             // head  
  36.             ImageView head = new ImageView(mContext);  
  37.             head.setId(1);  
  38.               
  39.             RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(  
  40.                     6464);  
  41.             mLayoutParams.topMargin = 15;  
  42.             mLayoutParams.leftMargin = 10;  
  43.             mLayoutParams.bottomMargin = 15;  
  44.             mLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);  
  45.             rl.addView(head, mLayoutParams);  
  46.   
  47.             // nickname  
  48.             TextView mTextView = new TextView(mContext);  
  49.             mTextView.setTextSize(20);  
  50.             mTextView.setTextColor(0xff000000);  
  51.             mTextView.setId(2);  
  52.   
  53.             mLayoutParams = new RelativeLayout.LayoutParams(  
  54.                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  55.             mLayoutParams.topMargin = 10;  
  56.             mLayoutParams.leftMargin = 10;  
  57.             mLayoutParams.addRule(RelativeLayout.RIGHT_OF, 1);  
  58.             rl.addView(mTextView, mLayoutParams);  
  59.   
  60.               
  61.             holder = new ViewHolder();  
  62.             holder.head = head;  
  63.             holder.name = mTextView;  
  64.             rl.setTag(holder);  
  65.         } else {  
  66.             holder = (ViewHolder) convertView.getTag();  
  67.             rl = (RelativeLayout) convertView;  
  68.         }  
  69.   
  70.         holder.head.setImageDrawable(bitmapDrawable_headimage);  
  71.         holder.name.setText(mVector.elementAt(position));  
  72.   
  73.         return rl;  
  74.     }  
  75. }  

如果对之前讲的相对布局看过的话,这里对自定义布局的写法应该不难理解。
定义了一个头像ImageView和一个名字TextView。让头像距离上下左各有一段距离,然后让名字在头像右边显示,并距离头像右一定距离。

关于布局背景图片的设置:rl.setBackgroundDrawable(bitmapDrawable_listitembg);设置的图片是从asset中读取得到的,头像的也是。

最后附上显示效果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值