android中的代码布局
前言: 习惯了用xml来写布局的,再用代码来写布局,恐怕会很类。但毕竟有时候,我们还是需要用到代码来写布局。 代码布局与xml布局有很多相似点,在大多数方法上都可以直接用,只有个别方法的写法不太一样,接下来,我将分几篇来介绍常用的几种控件。 以Android工程自带的main.xml为例,看看代码是如何实现的 一、设置当前布局 关于这个问题只针对初级水平的人员,其他人可略过。 我们知道,在初建一个activity的时候,程序会帮我们建好:
[java] view plain copy protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }setContentView(R.layout.main);就是设置当前布局的样式。 我们用代码写布局的话,这里就要传入一个View对象。实际上它就是线性布局,相对布局,文本框等的对象。比如我们创建了一个线性布局,那么我们这里就将它的传进去,例如:
[java] view plain copy LinearLayout mLinearLayout = new LinearLayout(this); setContentView(mLinearLayout); 二、线性布局线性布局是我们经常使用的,我们先从它入手来熟悉代码布局。 [java] view plain copy // 创建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 [java] view plain copy // 创建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是一样的。 下面是完整版代码:
[java] view plain copy 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是为了更好为我们编写代码布局服务,在以后的代码范例中,都会采用下面继承该类方式。
[java] view plain copy 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中加载进来的
上一篇,我们已经介绍了一点代码布局,接下来,我们接着介绍
一、常用组件设置 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的例子:
[java] view plain copy mLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); mLayoutParams.addRule(RelativeLayout.RIGHT_OF, ID_IMAGE_HEAD); mLayoutParams.topMargin = 5; mLinearLayout.addView(mTextView, mLayoutParams); 如果对于上一篇关于TextView组件位置的设置有印象的话,这里就不难理解了,它就是告诉父类对象,当前组件针对父类布局要放到什么位置。 关于EditText这个组件,如何设置最大输入字数,这里只给个例子: [java] view plain copy mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(8) }); 我这里设置最多可输入的字数为8个。 二、相对布局 除了经常使用的线性布局外,还常会用到相对布局,尤其是它的相对位置设计非常适合机型适配要求。 在写相对布局的xml时候,我们几乎要位每个最近设置ID标示,好告诉其他组件相对位置关系。在代码布局中也同样如此,需要为它们独立设置ID标示,而且 不要重复 ,否则位置关系就会错误。 下面,我们给出一段例子,方便大家理解: [java] view plain copy // 创建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); 效果图:
该文章转载自:http://blog.csdn.net/xyz_fly/article/details/7498205
