Android programming Dynamic Load Layout example detailed "with demo source" _android

Source: Internet
Author: User
Tags stub

This article illustrates the dynamic loading layout of Android programming. Share to everyone for your reference, specific as follows:

Since the previous time the project needs to be loaded on one page depending on the different buttons loaded on different layout pages, then think of using Tabhot. However, the graphic provided by the art of the interface is completely tabhot, so think of the dynamic loading method to solve this demand. Here I tidy up a bit, wrote a DEMO hope that we will take a little detour later.

First of all, we first draw the frame of the interface, the schematic diagram is as follows:

The Middle White department is a linear layout file, I like to paint the time with a different color to mark a layout, easy to view. The layout file code is as follows:

 <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout "xmlns:android=" Schemas.android.com/apk/res/android "android:orientation=" vertical "android:layout_width=" Fill_parent "Android: layout_height= "Fill_parent" > <linearlayout android:orientation= "Horizontal" android:layout_width= "WRAP_"
   Content "android:layout_height=" wrap_content > <button android:text= "Load ListView" android:id= "@+id/button01" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" > </Button> <button android: text= "Load another page" android:id= "@+id/button02" android:layout_width= "wrap_content" android:layout_height= "WRAP_" Content "></Button> </LinearLayout> <linearlayout android:id=" @+id/linearlayout01 "Android: Background= "#FFFFFF" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" ></LinearLayout > </LinearLayout> 

As can be seen from the above effect diagram, the white linear layout is used to dynamically load the incoming layout file. All right, let's do this. If the layout file is loaded dynamically. Let's take this effect step-by-step, first of all, to sketch out the required XML, which is divided into steps below.

Create a new layout for the ListView page with the following code:

<?xml version= "1.0" encoding= "UTF-8"?> <linearlayout android:id=
"@+id/layout"
 android:layout_ Width= "Fill_parent" android:layout_height= "fill_parent"
 xmlns:android= "http://schemas.android.com/apk/res/" Android ">
 <listview android:id=" @+id/listview01 "android:layout_width=" Wrap_content "
  android: layout_height= "Wrap_content" ></ListView>
</LinearLayout>

Create a new ListView for each row of data, the code is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <linearlayout android:id=
"@+id/linearlayout01"
 android: Layout_width= "Fill_parent" android:layout_height= "fill_parent"
 xmlns:android= "http://schemas.android.com/" Apk/res/android ">
 <textview android:text=" @+id/textview01 android:id= "@+id/textview01"
  android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" ></TextView>
</LinearLayout>

Create a new page that distinguishes this page from being dynamically loaded with the following code:

<?xml version= "1.0" encoding= "UTF-8"?> <linearlayout android:id=
"@+id/hellolayout"
 android: Layout_width= "Fill_parent" android:layout_height= "fill_parent"
 xmlns:android= "http://schemas.android.com/" Apk/res/android ">
 <textview android:text=" HELLO "
  android:layout_width=" Wrap_content "Android: layout_height= "Wrap_content" ></TextView>
</LinearLayout>

Implement ListView Data, here does not detail how to populate ListView each row of data, there are puzzled friends can view the previous ListView related articles, the code is as follows:

Package Com.terry;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.TextView; public class ListAdapter extends Baseadapter {arraylist 

The outline of the project is shown below:

Well, our preparation work has been completed, the next is to teach you how to achieve dynamic loading of the layout of the above page, first look at the effect of the picture:

Click on the first button

Click on the second button

The dynamic load code is as follows:

Package Com.terry;
Import android.app.Activity;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.LinearLayout;
Import Android.widget.ListView;
Import Android.widget.TextView; The public class Dynaactivity extends activity {/** called the ' when the ' is ' the activity ' is a./@Override public void on
  Create (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.main);
  Final Layoutinflater Inflater = Layoutinflater.from (this);
  Button btn = (button) Findviewbyid (R.ID.BUTTON01);
  Button btn2 = (button) Findviewbyid (R.ID.BUTTON02);
  Final LinearLayout lin = (linearlayout) Findviewbyid (r.id.linearlayout01); Btn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated Me Thod stub linearlayout layout = (linearlayout) inflater.inFlate (R.layout.listview, null). Findviewbyid (R.id.layout);
    ListView lv= (ListView) layout.getchildat (0);
    Lv.setadapter (New ListAdapter (Dynaactivity.this));
    Lin.removeallviews ();
   Lin.addview (layout);
  }
  }); Btn2.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated m Ethod stub linearlayout layout = (linearlayout) inflater.inflate (R.layout.hello, null). Findviewbyid (r.id.hellol
     Ayout);
     TextView lv= (TextView) layout.getchildat (0);
    Lv.settextcolor (color.red);
    Lin.removeallviews ();
   Lin.addview (layout);
 }
  });

 }
}

The above by using Layoutinflater every time you click the button to read the layout file, and then find the layout file inside the view of the operation after the view loaded into our setcontentview to put in the layout file, each dynamic load file must call The Removeallviews method clears the previously loaded View. Is it simple? Of course dynamic load view There are many ways to try different writing. May grasp the different experience, wish you to master the development technology of Android in the morning.

TIP: Because it is based on view operation, you can use Animation animation effect to make its replacement interface more natural, more ornamental.

Full example source click here to download the site .

More interested readers of Android content can view this site: "Android Layout Layout Skills Summary", "Android Development introduction and Advanced Course", "Android debugging techniques and common problems solution summary", " Android Multimedia How-to Summary (audio, video, audio, etc), summary of Android Basic components usage, Android View view tips and Android Control usage summary

I hope this article will help you with the Android program.

Related Article

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.