Dynamic addition of child controls and dynamic controls
I want to achieve this: click the button to dynamically generate the previously defined layout in xml.
The custom xml file rizhi_pinglun.xml:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/pinglun_layout" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: orientation = "horizontal" android: layout_margin = "10dp" android: layout_marginLeft = "30dp" android: background = "@ drawable/rizhi_background_white"> <TextView android: id = "@ + id/user1" android: layout_width = "wrap_content" Ndroid: layout_height = "wrap_content" android: text = "user1" android: textColor = "# 3333cc"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "comment:" android: layout_marginLeft = "3dp"/> <TextView android: id = "@ + id/huifu_content" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginLeft = "10dp" android: text = "good weather today ~ "Android: textColor =" #080808 "type =" codeph "text ="/codeph "/> </LinearLayout>
The original xml file is to insert the above xml into this layout: rizhi_test.xml:
<Pre name = "code" class = "html"> <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/rizhitest" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical" android: background = "# CAE1FF"> <TextView android: id = "@ + id/title" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: background = "@ drawable/rizhi_title" android: text = "xxx log" android: gravity = "center" android: textColor = "#454545" android: textSize = "25sp"/> <TextView android: id = "@ + id/rizhi_title" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: background = "# FFC0CB" android: text = "settled by dust," android: gravity = "center" android: textColor = "#454545" android: textSize = "25sp" android: layout_margin = "5dp"/> <RelativeLayout android: id = "@ + id/touxiang_layout" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: background = "@ drawable/rizhi_background_white" android: layout_margin = "5dp" android: orientation = "horizontal"> <ImageView android: id = "@ + id/touxiang" android: layout_width = "50dp" android: layout_height = "50dp" android: background = "@ drawable/touxiang"/> <RelativeLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginLeft = "70dp" android: background = "# FFC0CB" android: layout_marginTop = "5dp"> <TextView android: id = "@ + id/username" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "sunflower in the sun" android: textColor = "#454545" android: textSize = "15sp"/> <TextView android: id = "@ + id/rizhi_time" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "August 29, 2014" android: textColor = "#454545" android: textSize = "15sp" android: layout_below = "@ id/username" android: layout_marginTop = "3dp"/> </RelativeLayout> <Button android: id = "@ + id/button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "button"/> </LinearLayout>
</pre><p></p><pre>
Java file:
<span style="white-space:pre"></span>private Context context;private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.rizhi_test);context = this;button = (Button) findViewById(R.id.button);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {addMyView();}});}
Private View addMyView () {<span style = "white-space: pre"> </span> // (1) LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); // or LayoutInflater inflater = LayoutInflater. from (Activity. this); // or LayoutInflater inflater = getLayoutInflater (); // (2) View view = inflater. inflate (R. layout. rizhi_pinglun, null); // The layout you want to add // (3) LayoutParams liaParams = new LayoutParams (LinearLayout. layoutParams. WRAP_CONTENT, LinearLayout. layoutParams. WRAP_CONTENT); liaParams. setMargins (20, 10, 20, 10); // sets the left and right margins, but does not use rizhitest. addView (view, liaParams); // LinearLayout. layoutParams. WRAP_CONTENT ));}
Here, because the original Layout file is LinearLayout, the dynamically generated layout is located below all the controls of the original Layout. If you click the button, a new layout will be generated and will not overlap. Effect
There is a problem. If the original layout is RelativeLayout, the margin problem can be solved. However, if a problem occurs, the newly generated control will overwrite the originally generated control, which gives people the feeling that, only one control is generated. I don't know why. You are welcome to provide good suggestions.
RelativLayout xml file: rizhi_test.xml:
<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/rizhitest" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical" android: background = "# CAE1FF"> <TextView android: id = "@ + id/title" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: background = "@ drawable/rizhi_title" android: text = "xxx log" android: gravity = "center" android: textColor = "#454545" android: textSize = "25sp"/> <TextView android: id = "@ + id/rizhi_title" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: background = "# FFC0CB" android: text = "settled by dust," android: gravity = "center" android: textColor = "#454545" android: textSize = "25sp" android: layout_below = "@ id/title" android: layout_margin = "5dp"/> <RelativeLayout android: id = "@ + id/touxiang_layout" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: background = "@ drawable/resize" android: layout_margin = "5dp" android: orientation = "horizontal" android: layout_below = "@ id/rizhi_title"> <ImageView android: id = "@ + id/touxiang" android: layout_width = "50dp" android: layout_height = "50dp" android: background = "@ drawable/touxiang"/> <RelativeLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginLeft = "70dp" android: background = "# FFC0CB" android: layout_marginTop = "5dp"> <TextView android: id = "@ + id/username" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "sunflower in the sun" android: textColor = "#454545" android: textSize = "15sp"/> <TextView android: id = "@ + id/rizhi_time" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "August 29, 2014" android: textColor = "#454545" android: textSize = "15sp" android: layout_below = "@ id/username" android: layout_marginTop = "3dp"/> </RelativeLayout> <Button android: id = "@ + id/button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "button" android: layout_below = "@ id/touxiang_layout"/> </RelativeLayout>
private Context context;private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.rizhi_test);context = this;button = (Button) findViewById(R.id.button);final View nView = addMyView(button);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {addMyView(nView);}});}
Private View addMyView (View xdView) {// (1) LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); // or LayoutInflater inflater = LayoutInflater. from (Activity. this); // or LayoutInflater inflater = getLayoutInflater (); // (2) View view = inflater. inflate (R. layout. rizhi_pinglun, null); // (3) RelativeLayout rizhitest = (RelativeLayout) findViewById (R. id. rizhitest); RelativeLayout. layoutParams relParams = new LayoutParams (LinearLayout. layoutParams. WRAP_CONTENT, LinearLayout. layoutParams. WRAP_CONTENT); relParams. setMargins (20, 10, 20, 10); relParams. addRule (RelativeLayout. BELOW, xdView. getId (); rizhitest. addView (view, relParams); return view ;}
Test Results
Solution: How to dynamically insert controls in android
I wrote the code for you for half an hour and passed my test ~
Run the command to view the result ~ When you perform the key remove operation, you need to write comments to you ~
Layout file content of the layout:
----------------------------------------------------------------------------------
<? 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" android: id = "@ + id/linearlayout">
<LinearLayout android: id = "@ + id/LinearLayout01"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content">
<Button android: layout_height = "wrap_content" android: id = "@ + id/add"
Android: text = "Add" android: layout_width = "100px"> </Button>
<Button android: layout_height = "wrap_content"
Android: layout_width = "100px" android: text = "Remove" android: id = "@ + id/remove"> </Button>
</LinearLayout>
<TextView android: id = "@ + id/TextView01" android: text = "This is textView ."
Android: layout_width = "fill_parent" android: gravity = "center"
Android: layout_height = "50px"> </TextView>
</LinearLayout>
----------------------------------------------------------- ...... The remaining full text>
C # winform dynamically add controls
Private void button#click (object sender, EventArgs e)
{
Try
{
Int num = int. Parse (textBox1.Text );
For (int I = 0; I <num; I ++)
{
TextBox tb = new TextBox ();
// Set the location
Tb. Top = 50 + I * 30;
// Add controls
This. Controls. Add (tb );
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );;
}
}