Use Code Dynamic Layout and Android layout in android

Source: Internet
Author: User

Use Code Dynamic Layout and Android layout in android

Dynamic code layout in Android


This article describes how to use dynamic code layout in android. Sometimes, based on different requirements, such as the number of entries on the server, the page layout control (display number, icon, etc.) in the app is determined ). This section describes how to use java code for dynamic layout.


I ,:


You can find any image.


Ii. xml file Layout

<? Xml version = "1.0" encoding = "UTF-8"?> <ScrollView xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "@ android: color/white"> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "vertical"> <! -- The title bar can be customized here, because each page has a title, return, and so on --> <RelativeLayout android: id = "@ + id/layout_titlebar" android: layout_width = "match_parent" android: layout_height = "48dp" android: layout_marginBottom = "20dp" android: background = "# ed4255"> <TextView android: id = "@ + id/text_title" style = "@ style/Text. title "android: layout_width =" match_parent "android: layout_height =" match_parent "android: gravity =" center "android: text =" business function Introduction" /> </RelativeLayout> <! -- The child layout is dynamically generated by the code --> <LinearLayout android: id = "@ + id/layout_more" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" android: padding = "4dp"/> </LinearLayout> </ScrollView>


Iii. Child entry xml layout File

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="84dp"    android:layout_weight="1.0"    android:clickable="true" >    <ImageView        android:id="@+id/image_icon"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:layout_marginTop="16dp"        android:duplicateParentState="true"        android:src="@drawable/ic_department_01_normal" />    <TextView        android:id="@+id/text_title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal|bottom"        android:background="@null"        android:layout_marginBottom="6dp"        android:gravity="center"        android:duplicateParentState="true"        android:textColor="@drawable/text_service_color"        android:textSize="14dp" /></FrameLayout>




4. Dynamic java code Layout

/*** @ Author gao_chun ***/public class MainActivity extends Activity implements OnClickListener {private ViewGroup mMoreLayout; // parent layout container (the Dynamically Loaded resource images and text layout will be added to it)/* (non-Javadoc) * @ see app. ui. titleActivity # onCreate (android. OS. bundle) */@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); initUI (); // ensure the uniqueness of the startup method} private void initUI () {setContentView (R. layout. activity_main); // find the container (the control here is LinearLayout and converted to ViewGroup because ViewGroup is the base class of the container) mMoreLayout = (ViewGroup) findViewById (R. id. layout_more); // because the text is also dynamically generated, use the array file in android to define the resource file and retrieve final String [] categories = getResources (). getStringArray (R. array. categories); final int size = categories. length; // String [] length final int rowCount = size/3; // number of rows to be laid out (three rows in each row) /*** dynamically add layout method encapsulation * parameter 1. parent container 2. resource text array 3. from Beginning 4. number of rows */fillViews (mMoreLayout, categories, 0, rowCount);} private void fillViews (ViewGroup layout, String [] categories, int start, int end) {// The first line View of the table. inflate (this, R. layout. layout_line_horizonal, layout); for (int I = start; I <end; I ++) {// locate the index, it is easy to add image files and text final int firstIndex = I * 3; final int secondIndex = I * 3 + 1; final int thirdIndex = I * 3 + 2; final String firstCatego according to the index Ry = categories [firstIndex]; final String secondCategory = categories [secondIndex]; final String thirdCategory = categories [thirdIndex]; // The local image is loaded, use the app package to find the image resource file named by the rule // ---> because there are two effects, one is the default image, the second is to press the triggered image and text final int firstDrawableNormal = getResources (). getIdentifier (String. format ("ic_department _ % 02d_normal", firstIndex + 1), "drawable", getApplicationContext (). getPackageName (); final int seco NdDrawableNormal = getResources (). getIdentifier (String. format ("ic_department _ % 02d_normal", secondIndex + 1), "drawable", getApplicationContext (). getPackageName (); final int thirdDrawableNormal = getResources (). getIdentifier (String. format ("ic_department _ % 02d_normal", thirdIndex + 1), "drawable", getApplicationContext (). getPackageName (); final int firstDrawablePressed = getResources (). getIdentifier (St Ring. format ("ic_department _ % 02d_pressed", firstIndex + 1), "drawable", getApplicationContext (). getPackageName (); final int secondDrawablePressed = getResources (). getIdentifier (String. format ("ic_department _ % 02d_pressed", secondIndex + 1), "drawable", getApplicationContext (). getPackageName (); final int thirdDrawablePressed = getResources (). getIdentifier (String. format ("ic_department _ % 02d_pressed", thir DIndex + 1), "drawable", getApplicationContext (). getPackageName (); // put the default image found above and the image pressed into the StateListDrawable cache final StateListDrawable firstDrawable = new StateListDrawable (); firstDrawable. addState (new int [] {android. r. attr. state_pressed}, getResources (). getDrawable (firstDrawablePressed); firstDrawable. addState (new int [] {}, getResources (). getDrawable (firstDrawableNormal); final StateListDr Awable secondDrawable = new StateListDrawable (); secondDrawable. addState (new int [] {android. r. attr. state_pressed}, getResources (). getDrawable (secondDrawablePressed); secondDrawable. addState (new int [] {}, getResources (). getDrawable (secondDrawableNormal); final StateListDrawable thirdDrawable = new StateListDrawable (); thirdDrawable. addState (new int [] {android. r. attr. state_pressed}, getResources (). ge TDrawable (thirdDrawablePressed); thirdDrawable. addState (new int [] {}, getResources (). getDrawable (thirdDrawableNormal); // The parent layout final LinearLayout linearLayout = new LinearLayout (this); // The first child Layout View. inflate (this, R. layout. layout_line_vertical, linearLayout); View. inflate (this, R. layout. layout_department, linearLayout); View. inflate (this, R. layout. layout_line_vertical, linearLayout); // the second sub-layout, Vie. W. inflate (this, R. layout. layout_department, linearLayout); View. inflate (this, R. layout. layout_line_vertical, linearLayout); // View the third sub-layout. inflate (this, R. layout. layout_department, linearLayout); View. inflate (this, R. layout. layout_line_vertical, linearLayout); LayoutParams layoutParams = new LinearLayout. layoutParams (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT); layout. addView (linearLayout, LayoutParams); // The last line View of the table. inflate (this, R. layout. layout_line_horizonal, layout); // according to the index getChildAt to the specified position final View firstView = linearLayout. getChildAt (1); firstView. setTag (firstCategory); // you can set a tag to determine which firstView is clicked. setOnClickListener (this); // click final TextView firstTextView = (TextView) firstView. findViewById (R. id. text_title); firstTextView. setText (firstCategory); // sets the final ImageV text. Iew firstImageView = (ImageView) firstView. findViewById (R. id. image_icon); firstImageView. setImageDrawable (firstDrawable); // sets the cached image to final View secondView = linearLayout. getChildAt (3); secondView. setTag (secondCategory); secondView. setOnClickListener (this); final TextView secondTextView = (TextView) secondView. findViewById (R. id. text_title); secondTextView. setText (secondCategory); final ImageVie W secondImageView = (ImageView) secondView. findViewById (R. id. image_icon); secondImageView. setImageDrawable (secondDrawable); final View thirdView = linearLayout. getChildAt (5); thirdView. setTag (thirdCategory); thirdView. setOnClickListener (this); final TextView thirdTextView = (TextView) thirdView. findViewById (R. id. text_title); thirdTextView. setText (thirdCategory); final ImageView thirdImageView = (I MageView) thirdView. findViewById (R. id. image_icon); thirdImageView. setImageDrawable (thirdDrawable);}/* (non-Javadoc) * @ see app. ui. titleActivity # onClick (android. view. view) */@ Override public void onClick (View v) {final Object tag = v. getTag (); // locate the click position through the setTag. if (tag! = Null) {String department = (String) tag; Toast. makeText (this, department, 0). show () ;}// else ignored }}

In The onClick event, find the specific Layout that the user clicks Based on the Tag set during Layout.

Note:For details about the getResources (). getIdentifier method, refer:Http://blog.csdn.net/gao_chun/article/details/45891383



Download source code:Http://download.csdn.net/download/gao_chun/8740979



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.