Creating controls in Android from code

Source: Internet
Author: User

 Packagebvb.de.openadbwireless.circle;ImportAndroid.annotation.TargetApi;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.graphics.Color;ImportAndroid.os.Build;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.Button;Importandroid.widget.LinearLayout;ImportAndroid.widget.ListView;Importandroid.widget.RelativeLayout;ImportAndroid.widget.RelativeLayout.LayoutParams;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;Importjava.util.ArrayList;Importjava.util.List; Public classAddviewbyjavaextendsActivity {PrivateContext context = This; PrivateRelativelayout Rl_root; PrivateTextView TV; @TargetApi (build.version_codes. KITKAT) @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Outermost control RelativelayoutRl_root =Newrelativelayout (context); //Add a TextViewTV =NewTextView (context); Relativelayout.layoutparams Tvlayoutparams=Newrelativelayout.layoutparams (layoutparams.match_parent, layoutparams.wrap_content); //Set MarginTvlayoutparams.leftmargin = 50; //Set paddingTv.setpadding (10, 10, 10, 10); Tv.settext ("TextView");        Tv.setlayoutparams (Tvlayoutparams);        Rl_root.addview (TV); //Add a buttonButton BTN =NewButton (context); Btn.settext ("Button"); //Setting the rule: below the TextViewRelativelayout.layoutparams Layoutparams =Newrelativelayout.layoutparams (layoutparams.match_parent, layoutparams.wrap_content);        Layoutparams.addrule (Relativelayout.below, Tv.getid ());        Btn.setlayoutparams (Layoutparams);        Rl_root.addview (BTN); Btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {tv.settext (Getchild ());        }        }); //Add a linearlayout below the button, with 4 buttons in the horizontal placeLinearLayout LL =Newlinearlayout (context); Relativelayout.layoutparams layoutParams1=Newrelativelayout.layoutparams (layoutparams.match_parent, layoutparams.wrap_content);        Layoutparams1.addrule (Relativelayout.below, Btn.getid ());        Ll.setlayoutparams (LAYOUTPARAMS1);        Ll.setbackgroundcolor (Color.green); Ll.setpadding (10, 50, 0, 0);        Rl_root.addview (LL);        Ll.setorientation (linearlayout.horizontal); intBtncount = 4;  for(inti = 0; i < Btncount; i++) {            FinalButton Button =NewButton (context); Linearlayout.layoutparams params=NewLinearlayout.layoutparams (NewLinearlayout.layoutparams (0, layoutparams.wrap_content)); Params.weight= 1; Button.settext ("Button" +i);            Ll.addview (button, params); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {toast.maketext (context, Button.gettext (), Toast.length_short). Show ();        }            }); } ListView LV=NewListView (context); Relativelayout.layoutparams LAYOUTPARAMS2=Newrelativelayout.layoutparams (layoutparams.match_parent, layoutparams.wrap_content);        Layoutparams2.addrule (Relativelayout.below, Ll.getid ());        Lv.setlayoutparams (LAYOUTPARAMS2);        Rl_root.addview (LV); List<String> datas =NewArraylist<string>();  for(inti = 0; I < 20; i++) {Datas.add ("Name" +i); } lv.setadapter (NewArrayadapter<string>(Context, Android.) R.layout.simple_list_item_1, Android.        R.id.text1, datas));    Setcontentview (Rl_root); }     PublicString Getchild () {StringBuilder sb=NewStringBuilder ();  for(inti = 0; I < Rl_root.getchildcount (); i++) {sb.append (Rl_root.getchildat (i). toString ()). Append ("\ n"); }        returnsb.tostring (); }}

Creating controls in Android from code

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.