Android-dynamically add controls

Source: Internet
Author: User

[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 first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // custom EditText component EditTextTest ed = New EditTextTest (this); // set to input text ed from 140px on the left. setPadding (140, 0, 0, 0); // load a layout file and dynamically add an EditText RelativeLayout relativeLayout = (RelativeLayout) getLayoutInflater () to the layout (). inflate (R. layout. main, null); // create a LayoutParams object RelativeLayout. layoutParams layoutParams = new RelativeLayout. layoutParams (ViewGroup. layoutParams. FILL_PARENT, ViewGroup. layoutParams. WRAP_CONTENT); // set the android: layout_below Value of layoutParams. addRule (RelativeLayout. BELOW, R. id. button2); // dynamically add EditText relativeLayout. addView (ed, layoutParams); // setContentView (relativeLayout);} custom EditText control: [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. w Idget. editText; public class EditTextTest extends EditText {public EditTextTest (Context context) {super (context);} public EditTextTest (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);} public EditTextTest (Context context, AttributeSet attrs) {super (context, attrs) ;}@ Override protected void onDraw (Canvas canvas) {Paint paint = new Paint (); paint. setTextS Ize (20); paint. setColor (Color. GRAY); canvas. drawText ("input prompt text:", 10, 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" android: text = "Button"/> </RelativeLayout>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.