<relativelayout xmlns: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" >
<textview
Android:id= "@+id/top_tv"
Android:layout_width= "Match_parent"
android:layout_height= "30DP"
Android:background= "#ffffff"
android:gravity= "Center"
android:text= "Dynamic Layout in code"
Android:textcolor= "#7B68EE"/>
<linearlayout
Android:id= "@+id/child_layout"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/top_tv"
android:orientation= "Vertical" >
</LinearLayout>
</RelativeLayout>
--------------------------------------------------------------
public class Testactivity extends Activity implements Onclicklistener {
Private LinearLayout childlinlayout;
Private list<map<string, string>> viewlist = new arraylist<map<string, string>> ();
Private int[] Colors = {R.color.color1, R.color.color2, R.color.color3, R.color.color4, R.color.color5};
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.layout_main);
/**displaymetrics provides a general information about the display, such as display size, resolution, font, etc. */
Displaymetrics metrics = new Displaymetrics ();
/** get screen resolution and displaymetrics*/
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics);
/** set two width */
int width1 = (metrics.widthpixels * 4/7);
int width2 = Metrics.widthpixels * 3/7;
/** Setting Height */
int height = 80;
Childlinlayout = (linearlayout) Findviewbyid (r.id.child_layout);
Getlistdata ();
for (int i = 0; i < viewlist.size (); i++) {
/** dynamically Set Layout */
LinearLayout layout1 = new LinearLayout (this);
Layout1.setorientation (linearlayout.horizontal);
Layout1.setlayoutparams (New Layoutparams (Layoutparams.match_parent, layoutparams.match_parent));
int I1 = (i + 1) * 2-1;
int i2 = (i + 1) * 2;
TextView TV1 = new TextView (this);
Tv1.setid (I1);
Tv1.setonclicklistener (this);
Tv1.setgravity (gravity.center_vertical);
Tv1.settext (Viewlist.get (i). Get ("left"));
TextView TV2 = new TextView (this);
Tv2.setid (I2);
Tv2.setonclicklistener (this);
Tv2.setgravity (gravity.center_vertical);
Tv2.settext (Viewlist.get (i). Get ("right"));
if (i% 2 = = 0) {
Tv1.setwidth (WIDTH1);
Tv1.setheight (height);
Tv1.setbackgroundresource (Colors[0]);
Tv2.setwidth (WIDTH2);
Tv2.setheight (height);
Tv2.setbackgroundresource (Colors[1]);
}
else {
Tv1.setwidth (WIDTH2);
Tv1.setheight (height);
Tv1.setbackgroundresource (colors[2]);
Tv2.setwidth (WIDTH1);
Tv2.setheight (height);
Tv2.setbackgroundresource (Colors[4]);
}
Layout1.addview (TV1);
Layout1.addview (TV2);
Childlinlayout.addview (LAYOUT1);
}
}
/** adding text data */
private void Getlistdata () {
for (int i = 0; i < 6; i++) {
map<string, string> strmap = new hashmap<string, string> ();
Strmap.put ("left", "This is the data on the side" + (i + 1) + "leftmost");
Strmap.put ("Right", "This is the data on the left" + (i + 1) + "OK");
Viewlist.add (STRMAP);
}
}
@Override
public void OnClick (View v) {
TextView TV = (TextView) Findviewbyid (V.getid ());
String content = Tv.gettext (). toString ();
Switch (V.getid ()) {
Case 1:
Showtoastmsg (content);
Break
Case 2:
Showtoastmsg (content);
Break
Case 3:
Showtoastmsg (content);
Break
Case 4:
Showtoastmsg (content);
Break
Case 5:
Showtoastmsg (content);
Break
Case 6:
Showtoastmsg (content);
Break
Case 7:
Showtoastmsg (content);
Break
Case 8:
Showtoastmsg (content);
Break
Case 9:
Showtoastmsg (content);
Break
Case 10:
Showtoastmsg (content);
Break
Case 11:
Showtoastmsg (content);
Break
Case 12:
Showtoastmsg (content);
Break
}
}
private void Showtoastmsg (String content) {
Toast.maketext (testactivity.this, content + content, Toast.length_short). Show ();
}
}
--------Res/values/colors.xml-----------------------------
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<color name= "Color1" > #F0F0F0 </color>
<color name= "Color2" > #D0D0D0 </color>
<color name= "Color3" > #EC182E </color>
<color name= "Color4" > #C41e85 </color>
<color name= "Color5" > #8B008B </color>
</resources>
Android Basics: Implement interface dynamic layout in code