前言:
习惯了用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.
造成这个原因,是组件在父类中重复加载了相同的组件
例如:
- TextView mTextView = new TextView(this);
- mTextView.setText("hello world");
- LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
- // 第一次添加
- mLinearLayout.addView(mTextView, mLayoutParams);
- // 第二次添加
- 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的例子:
- mLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
- LayoutParams.WRAP_CONTENT);
- mLayoutParams.addRule(RelativeLayout.RIGHT_OF, ID_IMAGE_HEAD);
- mLayoutParams.topMargin = 5;
- mLinearLayout.addView(mTextView, mLayoutParams);
关于EditText这个组件,如何设置最大输入字数,这里只给个例子:
- mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(8) });
二、相对布局
除了经常使用的线性布局外,还常会用到相对布局,尤其是它的相对位置设计非常适合机型适配要求。
在写相对布局的xml时候,我们几乎要位每个最近设置ID标示,好告诉其他组件相对位置关系。在代码布局中也同样如此,需要为它们独立设置ID标示,而且不要重复,否则位置关系就会错误。
下面,我们给出一段例子,方便大家理解:
- // 创建RelativeLayout对象
- RelativeLayout mRelativeLayout = new RelativeLayout(this);
- mRelativeLayout.setLayoutParams(new LinearLayout.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
- // TextView组件:白色30号字
- TextView mTextView = new TextView(this);
- mTextView.setTextSize(30);
- mTextView.setTextColor(-1);
- mTextView.setText("hello world");
- // 为其设置唯一的id标示
- mTextView.setId(1);
- // 为其设置位置关系
- RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- // 距离顶部20
- mLayoutParams.topMargin = 20;
- // 距离左边20
- mLayoutParams.leftMargin = 20;
- // 添加组件
- mRelativeLayout.addView(mTextView, mLayoutParams);
- // TextView组件:红色20号字
- mTextView = new TextView(this);
- mTextView.setTextSize(20);
- mTextView.setTextColor(0xffff0000);
- mTextView.setText("hello world");
- // 为其设置唯一的id标示
- mTextView.setId(2);
- // 为其设置位置关系
- mLayoutParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- // 距离顶部20
- mLayoutParams.topMargin = 20;
- // 距离左边20
- mLayoutParams.leftMargin = 20;
- // 设置它在白色字的下面
- mLayoutParams.addRule(RelativeLayout.BELOW, 1);
- // 水平居中显示
- mLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
- // 添加组件
- mRelativeLayout.addView(mTextView, mLayoutParams);
效果图:
——————————————————————————————————————————————————
前两篇介绍了一般常用的布局组件写法,这一次,再来看看其他组件的
一、listview
listview也和大多数组件一样,一般会在这里难住的,就是自定义布局样式的时候。
- // 创建LinearLayout对象
- LinearLayout mRelativeLayout = new LinearLayout(this);
- mRelativeLayout.setLayoutParams(new LinearLayout.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
- // 创建ListView对象
- ListView mListView = new ListView(this);
- mListView.setAdapter(new ListAdapter(this, mVector));
- // 添加组件
- mRelativeLayout.addView(mListView, new LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
其中,ListAdapter为继承BaseAdapter的自定义类。
mVector是我们自定义的数据,里面包含了两个名字:小明,小刚。
接下来,我们看看重点的ListAdapter是如何自定义布局的
- public class ListAdapter extends BaseAdapter {
- private Vector<String> mVector;
- private Context mContext;
- public ListAdapter(Context mContext, Vector<String> mVector) {
- this.mContext = mContext;
- this.mVector = mVector;
- }
- public Object getItem(int position) {
- return null;
- }
- public long getItemId(int position) {
- return 0;
- }
- public int getCount() {
- return mVector.size();
- }
- class ViewHolder {
- ImageView head;
- TextView name;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder = null;
- RelativeLayout rl = null;
- if (convertView == null) {
- rl = new RelativeLayout(mContext);
- rl.setBackgroundDrawable(bitmapDrawable_listitembg);
- // head
- ImageView head = new ImageView(mContext);
- head.setId(1);
- RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(
- 64, 64);
- mLayoutParams.topMargin = 15;
- mLayoutParams.leftMargin = 10;
- mLayoutParams.bottomMargin = 15;
- mLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
- rl.addView(head, mLayoutParams);
- // nickname
- TextView mTextView = new TextView(mContext);
- mTextView.setTextSize(20);
- mTextView.setTextColor(0xff000000);
- mTextView.setId(2);
- mLayoutParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- mLayoutParams.topMargin = 10;
- mLayoutParams.leftMargin = 10;
- mLayoutParams.addRule(RelativeLayout.RIGHT_OF, 1);
- rl.addView(mTextView, mLayoutParams);
- holder = new ViewHolder();
- holder.head = head;
- holder.name = mTextView;
- rl.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- rl = (RelativeLayout) convertView;
- }
- holder.head.setImageDrawable(bitmapDrawable_headimage);
- holder.name.setText(mVector.elementAt(position));
- return rl;
- }
- }
如果对之前讲的相对布局看过的话,这里对自定义布局的写法应该不难理解。
定义了一个头像ImageView和一个名字TextView。让头像距离上下左各有一段距离,然后让名字在头像右边显示,并距离头像右一定距离。
关于布局背景图片的设置:rl.setBackgroundDrawable(bitmapDrawable_listitembg);设置的图片是从asset中读取得到的,头像的也是。
最后附上显示效果: