Android开发教程:动态添加控件

Android开发教程:动态添加控件

package com.mrzhu.edittest;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

public class EditTestActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //自定义的带提示文本的EditText组件
  EditTextTest ed = new EditTextTest(this);
  //设置从左边140px处开始输入文本
  ed.setPadding(140, 0, 0, 0);
  //装载一个布局文件,向这个布局中动态添加一个EditText
  RelativeLayout relativeLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.main, null);
  //创建一个LayoutParams对象
  RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  //设置android:layout_below属性的值
  layoutParams.addRule(RelativeLayout.BELOW, R.id.button2);
  //动态添加EditText
  relativeLayout.addView(ed, layoutParams);
  //设置布局
  setContentView(relativeLayout);
 }
}

Android开发教程:动态添加控件

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c33d7c08b5939aeb2a455b5888b81b97.html