Android Dynamic Build layout (multi-layered nesting)

Source: Internet
Author: User

In addition to the ability to load an XML file to display the layout, Android can also generate layout code and display the layout through the Setcontentview (view view) method. A separate layer of layout, such as a master layout plus a control (such as Button\imageview, etc.) is relatively straightforward to generate code dynamically, with only the sample code shown below:

Package Com.example.android_dongtaishengcheng;import Android.os.bundle;import Android.app.activity;import Android.content.intent;import Android.view.menu;import Android.view.view;import Android.widget.Button;import Android.widget.relativelayout;import Android.widget.toast;import Android.widget.RelativeLayout.LayoutParams;    public class Mainactivity extends activity{relativelayout relativelayout = null;        Private button button; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); relativelayout = NE W Relativelayout (this); Layoutparams params = new Layoutparams (relativelayout.layoutparams.match_parent,relativelayout.layoutparams.match_ PARENT); Relativelayout.setlayoutparams (params); Relativelayout.setbackgroundresource (R.color.back); Setcontentview (relativelayout); button = New button (this); Layoutparams params2 = new Layoutparams, Button.setlayoutparams (PARAMS2);p Arams2.addrule ( relativelayout.center_in_parent); Button.settext("Hello"); Relativelayout.addview (button); Button.setonclicklistener (new View.onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated method Stubstartactivity (New Intent (Mainactivity.this,secondactivity.clas    s));    }}); }}
Here's the point: Dynamic generation of multi-layered nesting layouts.

Scenario Description: The parent layout is a linear layout with sub-layouts arranged in a vertical direction, and the child layout is a linear layout, arranged horizontally.
In fact, it is very simple, the key point is how to control the main layout of the child layout of the line display, that is, the implementation of orientation = "vertical". You can add a layer of layout to the child layout, the following drawparent () method, which is used to generate a direct child layout of the parent layout, and the Drawview () method is used to generate a sub-layout of the direct sub-layout, which is also a multi-layered nesting. Can be implemented as an effect:


The post code is as follows:

/** * */package com.example.android_dongtaishengcheng;import android. R.integer;import Android.app.activity;import Android.content.context;import Android.os.bundle;import Android.view.gravity;import Android.view.view;import Android.widget.button;import android.widget.LinearLayout; Import Android.widget.relativelayout;import android.widget.linearlayout.layoutparams;/** * @author Zhiyuan * *    2014-5-29 a.m. 10:44:44 * */public Class Secondactivity extends activity{linearlayout layout = null;    LinearLayout line2 = null;    LinearLayout line3 = null;    LinearLayout line4 = null; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (saved instancestate); layout = new LinearLayout (this); Layoutparams params = new Layoutparams (layoutparams.match_parent, layoutparams.match_parent); layout.setlayoutparams (params); layout.setorientation (linearlayout.vertical); Setcontentview (layout);//Layout.addview (Drawview (this)); Line2 = (LinearlAyout) Drawparent (this), Line3 = (linearlayout) drawparent (this), line4 = (linearlayout) drawparent (this), for (int i = 0; i < 2; i++) {Line2.addview (Drawview (Secondactivity.this, i));} for (int i = 0; i < 4; i++) {Line3.addview (Drawview (Secondactivity.this, i));} for (int i = 0; i < 5; i++) {Line4.addview (Drawview (Secondactivity.this, i));}    Layout.addview (line2); Layout.addview (line3); Layout.addview (line4); }//Child layout of the generated sub-layout public View Drawview (context context, int count) {linearlayout layout = new LinearLayout (context); L Ayoutparams params = new Layoutparams (layoutparams.match_parent,layoutparams.wrap_content, 1);p arams.gravity =    Gravity.center;layout.setorientation (linearlayout.horizontal); Layout.setlayoutparams (params); if (count = = 1) { Layout.setbackgroundresource (r.color.back);} Relativelayout relativelayout = new Relativelayout (context); Android.widget.RelativeLayout.LayoutParams params3 = new Android.widget.RelativeLayout.LayoutParams (android.widget.RelativeLaYout. Layoutparams.match_parent,android.widget.relativelayout.layoutparams.wrap_content); Relativelayout.setlayoutparams (PARAMS3);/* * ImageView ImageView = new ImageView (context); Layoutparams params2 = new Layoutparams (layoutparams.wrap_content, layoutparams.wrap_content); */button ImageView = new Button (context); Android.widget.RelativeLayout.LayoutParams params2 = new Android.widget.RelativeLayout.LayoutParams (Android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT, Android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);//imageview.setgravity (Relativelayout.center_in_ PARENT); Imageview.settext ("test");p Arams2.addrule (relativelayout.center_in_parent);//        Imageview.setbackgroundresource (R.drawable.ic_launcher); Imageview.setlayoutparams (PARAMS2); /* * TextView TextView = new TextView (context); Layoutparams params3 = new Layoutparams (layoutparams.wrap_content, layoutparams.wrap_content); Textview.settext ("Test-specific"); * Textview.setlayoutparams (PARAMS3); */relativelayout.addview (ImageView);//Layout.addview (TextView);    Layout.addview (relativelayout); return layout; }//Generate a sub-layout of the main layout public View drawparent (context context) {LinearLayout layout = new LinearLayout (context); Layoutparams params = new Layoutparams (layoutparams.match_parent, layoutparams.wrap_content);//params.gravity = Gravity.center_horizontal;layout.setorientation (linearlayout.horizontal); Layout.setlayoutparams (params); return    Layout }}

Attached demo:http://download.csdn.net/detail/laoziyueguo3/7423939

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.