歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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);
 }
}

 

Copyright © Linux教程網 All Rights Reserved