[html] [html] 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 FIR St created. */ @Override public void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); //Custom EDITTEXT components with hint text edittexttest ed = new Edittextt EST (this); //settings start at the left 140px input text ed.setpadding (140, 0, 0, 0); //loading a layout file to dynamically add a edittext Relativelayout to this layout Relativelayout = (relativelayout) getlayoutinflater (). Inflate (R.layout.main, nULL); //Create a Layoutparams object Relativelayout.layoutparams Layoutparams = new Relativelayout.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_ CONTENT); //Setting the value of the Android:layout_below property Layoutparams.addrule (Relativelayout.below, R.id.button2); //Dynamic add EditText Relativelayout.addview (Ed, Layoutparams); //Set layout Setcontentview (relativelayout); } Custom EditText controls: [HTML] www.2cto.compackage com.mrzhu.edittest; import Android.content.Context; import Android.graphics.Canvas; import Android.graphics.Color; import Android.graphics.Paint; import Android.util.AttributeSet; import Android.widget.EditText; &Nbsp;public class Edittexttest extends EditText { public edittexttest (context context) {& nbsp; super (context); Public edittexttest (context context, AttributeSet att RS, int defstyle) { Super (context, Attrs, Defstyle), } & nbsp; Public edittexttest (context context, AttributeSet attrs) { SUPER (cont ext, attrs); @Override protected void OnDraw (canvas canvas) { Paint paint = new paint (); paint.settextsize (20); Paint.setcolor (Color.gray); canvas.drawtext ("Input hint text:", ten, GetHeight ()/2 + 5, paint); Super.ondraw (canvas); } } main.xml:[html] <?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id /linearlayout " android:layout_width=" fill_parent " android:layout_height=" Fill_parent " android:orientation=" Horizontal "> <button android:id= "@+id/button2" android:layout_width= " 80DP android:layout_height= "80DP" Android:layout_ Alignparenttop= "true" android:layout_centerhorizontal= "true" android:layout_margintop= "150DP" android:onclick= "click" & nbsp android:text= "button"/> </relAtivelayout>
android--adding controls dynamically