Android dynamically set layout with Java (add Delete to modify layout)

Source: Internet
Author: User

XML is very convenient for developers, not only easy to use, but also able to debug in a timely manner, after modifying the interface to see the effect immediately.

The Java Setup layout does not have this advantage. But Java can dynamically manipulate the layout, which is 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 modifies the layout property, on the other hand is adds and deletes the control.


First of all, let's talk about the application of dynamic layout in the project, take the Gold map For example, such as:



We can see that the default interface of the gold map is not the same as the interface after clicking on the map, and the same control is not in the same position in layout, which is difficult to implement with XML. So the Java dynamic layout has its importance.


Next look at the author to share the demo effect:(source at the end of the article)



The code is 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 the layout Show Private button bt_add;//add layout private button bt_delete;//Delete layout private relativelayout R    L_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); The control here to get additional XML needs to first introduce the layout view (this linearlayout is used to demonstrate adding and removing) 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 ()) {R.id.button1:rl_ Infotip.setvisibility (View.gone);//Bottom tip set invisible//Initialize width high property relativelayout.layoutparams LP1 = NE               W Relativelayout.layoutparams (         ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);                Lp1.addrule (Relativelayout.align_parent_bottom);//Set Bottom Lp1.setmargins (10, 0, 0, 10);//set margin, here in px            Rl_radiogroup.setlayoutparams (LP1);//dynamic change of layout break;                Case R.id.button2:rl_infotip.setvisibility (view.visible);//Bottom tip set visible//Initialize Width height property Relativelayout.layoutparams LP2 = new Relativelayout.layoutparams (ViewGroup.LayoutParams.WRAP_C                Ontent, ViewGroup.LayoutParams.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 the control Rl_radiogroup.setlayoutparams (LP2) above r.id.info_tip;//dynamic change layout break; Case R.ID.BUTTON3://Initialize the width-height property, here in px relativelayout.layoutparams lp3 = new relativelayout.la         Youtparams (200, 200);       Lp3.addrule (Relativelayout.below, r.id.button4);//Set BELOW, let the control under R.id.button4 Rl_main.addview (ll_test            , lp3);//dynamic Change Layout ll_test.setvisibility (view.visible);//You need to set the layout display here, otherwise it will not show break;        Case R.id.button4:rl_main.removeview (ll_test);//dynamic change of 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_he ight= "wrap_content" android:text= "Hide"/> <button android:id= "@+id/button2" Android:layout_bel ow= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:te Xt= "Show"/> <button android:id= "@+id/button3" android:layout_below= "@+id/button2" Android:layo Ut_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Add Layout"/> <button A Ndroid:id= "@+id/button4" android:layout_below= "@+id/button3" android:layout_width= "Wrap_content" Android oid:layout_height= "Wrap_content"       android:text= "Delete Layout"/> <radiogroup android:id= "@+id/radio_group" android:layout_width= "Wrap_c        Ontent "android:layout_height=" wrap_content "android:padding=" 5DP "android:layout_marginleft=" 10px " Android:layout_marginbottom= "10px" android:orientation= "horizontal" android:layout_above= "@+id/info_ti P "android:background=" @android: Color/darker_gray "> <textview android:layout_width=            "Wrap_content" android:layout_height= "wrap_content" android:text= "accuracy:"/> <radiobutton Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:checked=            "True" android:text= "normal" android:textcolor= "@android: Color/black"/> <radiobutton            Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "precision" Android:textcoloR= "@android: Color/black"/> </RadioGroup> <relativelayout android:id= "@+id/info_tip" Andro        Id:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:layout_alignparentbottom= "true" android:paddingleft= "10DP" android:paddingright= "10DP" android:paddingtop= "20DP" android:backg Round= "@android: Color/darker_gray" > <textview android:id= "@+id/info_tip_name" a            Ndroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Disaster Site" Android:textcolor= "@android: Color/black" android:textsize= "20DP"/> <textview Android : id= "@+id/info_tip_distance" android:layout_below= "@+id/info_tip_name" android:layout_width= "Wrap_co            Ntent "android:layout_height=" wrap_content "android:text=" Disaster distance "/> <textview Android:id= "@+id/info_tiP_address "android:layout_torightof=" @+id/info_tip_distance "android:layout_below=" @+id/info_tip_nam E "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:layou t_marginleft= "10DP" android:text= "Disaster address"/> <button 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=" 10DP "Androi d:orientation= "Horizontal" > <button android:layout_width= "0DP" Android:layo ut_weight= "1" android:layout_height= "wrap_content" android:text= "Drive"/> <but Ton AndroId:layout_width= "0DP" android:layout_weight= "1" android:layout_height= "Wrap_content" android:text= "Bus"/> <button android:layout_width= "0DP" Android:layo ut_weight= "1" android:layout_height= "wrap_content" android:text= "Walk"/> </linear Layout> </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    " ></LinearLayout>


Source Address: http://download.csdn.net/detail/double2hao/9428144


Android dynamically set layout with Java (add Delete to modify layout)

Related Article

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.