Android--Add layouts dynamically

Source: Internet
Author: User

In the project, encountered ScrollView and the use of the combination of the ListView, resulting in a sliding chaos, but there is a way to solve the problem, is the height of the handwritten listview, there is another way, the Portal: Android- Nesting listview in ScrollView.

But in the project, our ScrollView nested in these two ListView, this is more troublesome, in order not to use two of the above methods, we will rewrite another listview as a dynamic loading layout of the method to achieve, in the layout and other operations like the ListView is similar to, But there is no adapter.

Sub-layout

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal" >    <ImageViewAndroid:id= "@+id/img"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:contentdescription= "@string/action_settings"android:src= "@drawable/ic_launcher" />    <TextViewAndroid:id= "@+id/txt"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/app_name" /></LinearLayout>

Show layout

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " >    <ScrollViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" >        <LinearLayoutAndroid:id= "@+id/lay"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical" >        </LinearLayout>    </ScrollView>    <ButtonAndroid:id= "@+id/btn_add"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:onclick= "Click_add"Android:text= "@string/app_name" /></LinearLayout>

Code implementation

 Public classMainactivityextendsActivity {PrivateLinearLayout Lay; PrivateLinearLayout Item; PrivateImageView img; PrivateTextView txt; PrivateButton Btn_add; Private int[] pic ={r.drawable.ic_launcher, r.drawable.maps, R.drawable.appstore, R.drawable.calculator, R.drawable.camera}    ; PrivateString[] Str_pic = {"Ic_launcher", "Maps", "AppStore",            "Calculator", "Camera" }; Privatestring[] str = {"1", "2", "3", "4", "5" }; Private intTime = 0; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Lay=(LinearLayout) Findviewbyid (R.id.lay); Btn_add=(Button) Findviewbyid (R.id.btn_add); Btn_add.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Try{inflateandfind (); } Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace ();    }            }        }); }    Private voidInflateandfind ()throwsException {Item=(LinearLayout) view.inflate (Getbasecontext (), R.layout.item,NULL); IMG=(ImageView) Item.findviewbyid (r.id.img); TXT=(TextView) Item.findviewbyid (r.id.txt); if(Time < 5) {Class<com.yydcdut.layout.R.drawable> cls = r.drawable.class; intValue = Cls.getdeclaredfield (Str_pic[time]). GETINT (NULL); //Img.setimageresource (Pic[time]);Img.setimageresource (value);            Txt.settext (Str[time]);        Lay.addview (item); } Else Time= 0; time++; }}

Code parsing

In fact, the method is to use the inflate method to add a new layout one by one, inflate in the Android call Pump ha, is the layout of one by one dozen up.

There is also a Class<com.yydcdut.layout.r.drawable>, this is the name to get the ID of the int value, it should be the reflection mechanism of Java ~

I'm the dividing line of the king of the Land Tiger.

Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01

Layout.zip

Reprint Please specify source: Http://www.cnblogs.com/yydcdut

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.