In Android development, we often encounter situations where the interface layout controls are not deterministic. Due to some of the reasons for some features or to reflect the characteristics of some apps and other factors such as this will cause us to implement the interface layout need to dynamically load some controls, then the following to introduce how to use dynamic loading control to easily realize the function of the friends in QQ, It also mentions how to dynamically load an XML configuration file.
So to realize the function of the friend impression, we need to pass the following steps:
1. The interface initially needs to load a edittext and button control to fill in the Friends ' impressions and add friends ' impressions;
2. Need to create a new arrays.xml, in the XML file to add the friend impression tag background color;
3. Load the XML file in the activity, get the color in the file, and add an event listener for the button control, enabling you to automatically generate a friend impression label with a background color when clicked.
Follow these three steps to see the following code:
Only four colors are defined in this configuration file
Arrays.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string-array name= "Colorsarray" > <item > #ff78ff </item> <item > #abcd12 </item> <item > #cdba34 </item> <item > #345677 </item> </string-array></resources>
Let's see how to load the configuration files and controls
Package Com.example.sundyandroidtest;import Java.util.random;import Android.app.activity;import Android.graphics.color;import Android.os.bundle;import Android.view.view;import Android.view.View.OnClickListener ; Import Android.widget.button;import Android.widget.edittext;import Android.widget.linearlayout;import Android.widget.linearlayout.layoutparams;import Android.widget.textview;public class AutoColorShowActivity extends activity{//Save the color string in the XML file string[] acolors;//declares a linear layout linearlayout mlayout = null;// Declares the width and heightlinearlayout.layoutparams of the linear layout lpff;//the width and heightlinearlayout.layoutparams of the control lpww;// Declare friend impression label TextView Colortextview = null;//declaration Add button Butadd = null;//Declare friend impression evaluation input box EditText EditText = null;//declare random number, Used for random label colors random rand = null; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( savedinstancestate);//Gets the color string array acolors = Getresources (). Getstringarray (R.array.colorsarray); mlayout = new LinearLayout (this);//Set layout properties Mlayout.setorientation (Linearlayout.vertICAL); Lpff = new Linearlayout.layoutparams (layoutparams.match_parent,layoutparams.match_parent); LpWW = new Linearlayout.layoutparams (layoutparams.match_parent,layoutparams.wrap_content); MLayout.setLayoutParams (LpFF); /Add control to Layout EditText = new EditText (this); Mlayout.addview (EditText, LPWW); butadd = new Button (this); Butadd.settext ("Add") ; Mlayout.addview (Butadd, LPWW); Setcontentview (mlayout); rand = new Random (); Butadd.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//generates a random number between 0 and the length of the array int tag = Rand.nextint (acolors.length-1);// Get an impression of a friend entered in EditText string info = Edittext.gettext (). toString ();//Generate a text label dynamically and set the text and color for the label Colortextview = new TextView (autocolorshowactivity.this); Colortextview.settext (info); Colortextview.settextcolor (Color.Black); Colortextview.settextsize (30f); Colortextview.setbackgroundcolor (Color.parsecolor (Acolors[tag)); Mlayout.addview (Colortextview, LPWW); Edittext.settext ("");//Update layout mlayout.refreshdrawablestate ();}});}}
Android dynamically load XML files and controls to easily realize the function of QQ friends ' impressions