android编程之代码布局(一)

来源:互联网 发布:node实现反向代理跨域 编辑:程序博客网 时间:2024/04/30 08:30

前言:

习惯了用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 {@Overrideprotected void initRecourse() {// TODO Auto-generated method stub}@Overrideprotected void initData() {// TODO Auto-generated method stub}@Overrideprotected 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中加载进来的


android编程之代码布局(一)

原创粉丝点击