New UI-Java code dynamically add controls or xml layout, ui-javaxml

Source: Internet
Author: User

New UI-Java code dynamically add controls or xml layout, ui-javaxml

New UI-Java code dynamically add controls or xml la s

-- Reprinted please indicate the source: coder-pig. You are welcome to repost it. Please do not use it for commercial purposes!


The piggy Android development and exchange group has been established. You are welcome to join us. You can be a newbie, cainiao, or a great god.

After all, the power is limited. There will certainly be a lot of flaws in writing. You are welcome to point out, brainstorm, And let pig's blog post.

For more details, help more people, O (∩ _ ∩) O thank you!

Piggy Android Development Exchange Group:Piggy Android Development GroupGroup Number:421858269

New Android UI instance catalog:Http://blog.csdn.net/coder_pig/article/details/42145907



This section introduces:

In the previous section, we learned pure Java code to load the layout. Now we have a little Dynamic Layout Foundation. In this section

We will explain how to dynamically Add a View control based on xml! And dynamic loading of XML

Layout!



Body of this section:


1. Add controls dynamically in Java code:

There are two ways to add components dynamically:SetContentView (R. layout. activity_main );

The following shows how to dynamically Add a Button to the interface.

The layout of the activity_main.xml file is as follows:

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/RelativeLayout1" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "com. jay. example. trendsinflateviewdemo. mainActivity "> <TextView android: id =" @ + id/txtTitle "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: text = "I am the layout for loading xml files"/> </RelativeLayout>

First: setContentView () is not required ();

Package com. jay. example. trendsinflateviewdemo; import android. app. activity; import android. OS. bundle; import android. view. layoutInflater; import android. view. viewGroup. layoutParams; import android. widget. button; import android. widget. relativeLayout; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); Button btnOne = new Button (this); btnOne. setText ("I Am a dynamically added button"); RelativeLayout. layoutParams lp2 = new RelativeLayout. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); lp2.addRule (RelativeLayout. CENTER_IN_PARENT); LayoutInflater inflater = LayoutInflater. from (this); RelativeLayout rly = (RelativeLayout) inflater. inflate (R. layout. activity_main, null ). findViewById (R. id. relativeLayout1); rly. addView (btnOne, lp2); setContentView (rly );}}

Type 2: setContentView ();

Package com. jay. example. trendsinflateviewdemo; import android. app. activity; import android. OS. bundle; import android. view. viewGroup. layoutParams; import android. widget. button; import android. widget. relativeLayout; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button btnOne = new Button (this); btnOne. setText ("I Am a dynamically added button"); RelativeLayout. layoutParams lp2 = new RelativeLayout. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); lp2.addRule (RelativeLayout. CENTER_IN_PARENT); RelativeLayout rly = (RelativeLayout) findViewById (R. id. relativeLayout1); rly. addView (btnOne, lp2 );}}



Analysis summary:

The code is very simple. After the Button is created, we create a LayoutParams object to set the size of the Button.

The addRule () method sets the Button position!

Method 1:PassInflate () method of LayoutInflateLoaded the activity_main layout, obtained the outer container, and then

AddView Add button to container, and setContentView ();

Method 2:Because we have loaded the layout through the setContetView () method, now we canFindViewById

Find this outer container, AddView, and setContentView!

In addition, the view node set by setContentView () is the root node of the entire XML file!



It is not difficult to add controls dynamically. If you read the previous article <pure Java loading layout>,



2. dynamically load xml layout with Java code


Next, let's change one. The xml file is loaded this time! Add xml files dynamically!

Write down the layout file first:

Activity_main.xml:

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/RelativeLayout1" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "com. jay. example. trendsinflateviewdemo. mainActivity "> <Button android: id =" @ + id/btnLoad "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: text = "dynamic loading layout"/> </RelativeLayout>

Dynamically Loaded xml file inflate. xml:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: gravity = "center" android: orientation = "vertical" android: id = "@ + id/ly_inflate"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "I'm a Java code loading layout"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "I am a small button in the layout"/> </LinearLayout>

Next we will go to MainActivity. java:

Package com. jay. example. trendsinflateviewdemo; import android. app. activity; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup. layoutParams; import android. widget. button; import android. widget. linearLayout; import android. widget. relativeLayout; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the LayoutInflater object; final LayoutInflater inflater = LayoutInflater. from (this); // obtain the external container object final RelativeLayout rly = (RelativeLayout) findViewById (R. id. relativeLayout1); Button btnLoad = (Button) findViewById (R. id. btnLoad); btnLoad. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// load the layout object LinearLayout ly = (LinearLayout) inflater. inflate (R. layout. inflate_ly, null, false ). findViewById (R. id. ly_inflate); // sets the size and position of the loaded layout. layoutParams lp = new RelativeLayout. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); lp. addRule (RelativeLayout. CENTER_IN_PARENT); rly. addView (ly, lp );}});}}

Run:



Next we will analyze the Code:

① Get the container object:

Final RelativeLayout rly = (RelativeLayout) findViewById (R. id. RelativeLayout1 );

② Obtain the Inflater object, load the xml of the added layout, and find the root node of the outermost layer through findViewById

FInal LayoutInflater inflater = LayoutInflater. from (this );

LinearLayout ly = (LinearLayout) inflater. inflate (
R. layout. inflate_ly, null, false). findViewById (
R. id. ly_inflate );

③ Set the size and location information for the container:

RelativeLayout. LayoutParams lp = new RelativeLayout. LayoutParams (
LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT );
Lp. addRule (RelativeLayout. CENTER_IN_PARENT );

④ Add to outer container:

Rly. addView (ly, lp );





Now, let's talk about how to dynamically add controls or XML content through Java code!

In addition, after addView (), when this View does not need to be used, you can use RemoveView ()

This View is removed!





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.