Android Java Dynamic Add delete modify Layout _android

Source: Internet
Author: User

XML is very convenient for developers, not only easy to use, but also can be debugged in time, modify the interface immediately after the effect can be seen.
The Java settings layout does not have this advantage. But Java can dynamically manipulate the layout, something that XML cannot do. The author thinks, the Novice asks to master the Java dynamic setting layout mainly has two points, on the one hand is changes to the layout attribute, on the other hand is adds and deletes the control.

First of all, the dynamic layout in the application of the project, take the map of the High German as an example, the following figure:

We can see that the default interface of the gold map and the interface after the click of the map is not the same, the same control in the layout position is not the same, this use of XML is difficult to achieve, so the Java dynamic Settings layout has its importance.

Next, take a look at the shared demo effect:

The code is relatively easy to understand, the specific explanation has been commented in the code, the reader can write their own understanding.
Mainactivity:

Package com.example.activeuitest; 
Import android.support.v7.app.AppCompatActivity; 
Import Android.os.Bundle; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.Button; 
Import Android.widget.LinearLayout; 
Import Android.widget.RadioGroup; 
 
Import Android.widget.RelativeLayout; 
  public class Mainactivity extends appcompatactivity implements view.onclicklistener{private Button bt_gone;//Let layout hide Private button bt_visiable;//Let layout display private button bt_add;//add layout private button bt_delete;//Delete layout private Relat 
  Ivelayout Rl_main; 
  Private Radiogroup Rl_radiogroup; 
  Private Relativelayout Rl_infotip; 
 
  Private LinearLayout ll_test; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
    Setcontentview (R.layout.activity_main); 
    Init ();//Initialize} private void init () {bt_gone= (Button) Findviewbyid (R.id.button1); Bt_visiabLe= (Button) Findviewbyid (R.id.button2); 
    Bt_add= (Button) Findviewbyid (R.id.button3); 
 
    Bt_delete= (Button) Findviewbyid (R.ID.BUTTON4); 
    Rl_main= (relativelayout) Findviewbyid (r.id.main_layout); 
    rl_radiogroup= (Radiogroup) Findviewbyid (R.id.radio_group); 
 
    rl_infotip= (relativelayout) Findviewbyid (R.id.info_tip); To get other XML controls here, you need to first introduce a layout view (this linearlayout for adding and deleting) view view= Layoutinflater.from (this). Inflate ( 
    R.layout.test_linear_layout,null,false); 
 
    Ll_test= (LinearLayout) View.findviewbyid (r.id.test_layout); 
    Bt_gone.setonclicklistener (this); 
    Bt_visiable.setonclicklistener (this); 
    Bt_add.setonclicklistener (this); 
  Bt_delete.setonclicklistener (this); @Override public void OnClick (View v) {switch (V.getid ()) {case r.id.button1:rl_infotip.se Tvisibility (View.gone);//Bottom tip set invisible//Initialize width high property relativelayout.layoutparams LP1 = new Relativelayout.layou Tparams (viewgroup.layoutParams.wrap_content, ViewGroup.LayoutParams.WRAP_CONTENT); Lp1.addrule (Relativelayout.align_parent_bottom)//Set base lp1.setmargins (10, 0, 0, 10);//set margin, here is px R 
      L_radiogroup.setlayoutparams (LP1);//dynamic change layout break; Case R.id.button2:rl_infotip.setvisibility (view.visible);//Bottom Tip sets the visible//initialized width-high attribute relativelayout. Layoutparams LP2 = new Relativelayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, Viewgroup.layoutpara Ms. 
        Wrap_content); Lp2.setmargins (10, 0, 0, 10);/set margin, where the unit is PX lp2.addrule (relativelayout.above, r.id.info_tip);//Set ABOVE, let control in R. 
      Id.info_tip above Rl_radiogroup.setlayoutparams (LP2);//dynamic change layout break; Case R.ID.BUTTON3://Initialization width High property, here is px relativelayout.layoutparams lp3 = new Relativelayout.layoutparams (2 
        00, 200); Lp3.addrule (Relativelayout.below, r.id.button4);//Set BELOW, let control under R.id.button4 Rl_main.addview (Ll_test, LP3); dynamicChange Layout ll_test.setvisibility (view.visible);//You need to set the layout display here, otherwise the break will not appear; 
    Case R.id.button4:rl_main.removeview (ll_test);//dynamic change layout break;  } 
  } 
}

Activity_main:

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" 
   Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:id=" @+id/main_layout " > <button android:id= "@+id/button1" android:layout_width= "Wrap_content" Android:layout_heig ht= "wrap_content" android:text= "Hide"/> <button android:id= "@+id/button2" android:layout_below= "@+id" 
  /button1 "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" Display "/>" <button android:id= "@+id/button3" android:layout_below= @+id/button2 "android:layout_width=" Wrap_conte 
    NT "android:layout_height=" wrap_content "android:text= add Layout"/> <button android:id= "@+id/button4"  
    android:layout_below= "@+id/button3" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "Remove Layout"/> 
  <radiogroup android:id= "@+id/radio_group" android:layout_width= "Wrap_content" android:layout_height= 
    "Wrap_content" android:padding= "5DP" android:layout_marginleft= "10px" android:layout_marginbottom= "10px" android:orientation= "Horizontal" android:layout_above= "@+id/info_tip" android:background= "@android: Color/dark Er_gray "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_conte NT "android:text=" precision: "/> <radiobutton android:layout_width=" wrap_content "Android:layou" t_height= "Wrap_content" android:checked= "true" android:text= "normal" android:textcolor= "@android: COLOR/BL 
      Ack "/> <radiobutton android:layout_width=" wrap_content "android:layout_height=" "Wrap_content" android:text= "precision" android:textcolor= "@android: Color/black"/> </RadioGroup> <relativelayo UT android:id= "@+id/info_tip "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:layout_alignPar" Entbottom= "true" android:paddingleft= "10DP" android:paddingright= "10DP" android:paddingtop= "20DP" Andr Oid:background= "@android: Color/darker_gray" > <textview android:id= "@+id/info_tip_name" an Droid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Disaster location" Android:te Xtcolor= "@android: Color/black" android:textsize= "20DP"/> <textview android:id= "@+id/info_tip_dist ance "android:layout_below=" @+id/info_tip_name "android:layout_width=" Wrap_content "Android:layout_he" ight= "wrap_content" android:text= "Disaster Distance"/> <textview android:id= "@+id/info_tip_address" and roid:layout_torightof= "@+id/info_tip_distance" android:layout_below= "@+id/info_tip_name" Android:layout_widt 
      H= "Wrap_content"android:layout_height= "Wrap_content" android:layout_marginleft= "10DP" android:text= "Disaster address"/> <Bu Tton android:layout_alignparentright= "true" android:layout_width= "Wrap_content" android:layout_height 
      = "Wrap_content" android:text= "Details"/> <linearlayout android:layout_below= "@+id/info_tip_address" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_margintop= "10 DP "android:orientation=" horizontal "> <button android:layout_width=" 0DP "Android:lay out_weight= "1" android:layout_height= "wrap_content" android:text= "Drive"/> <button an Droid:layout_width= "0DP" android:layout_weight= "1" android:layout_height= "Wrap_content" Android : text= "Bus"/> <button android:layout_width= "0DP" android:layout_weight= "1" android: Layout_height= "Wrap_contEnt "android:text=" Walk "/> </LinearLayout> </RelativeLayout> </RelativeLayout>  

Test_linear_layout:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
  "http://schemas.android.com/" Apk/res/android " 
  android:layout_width=" 200DP " 
  android:layout_height=" 200DP " 
  android:background=" @ Android:color/holo_blue_bright " 
  android:id=" @+id/test_layout " 
  android:orientation=" Horizontal 
  " > 
 

The above is the entire content of this article, I hope to help you learn.

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.