android--Adding controls dynamically

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 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

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.