In the development of the app, sometimes a layout will be used repeatedly, you can use the repeatedly used layout to write an XML file, when to use the includ tag to introduce XML
Here is an XML that I've written repeatedly, with 2 buttons, a TextView and a ProgressBar
Layout_progress.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:gravity= "Center"
Android:visibility= "Gone" >
<progressbar
Android:id= "@+id/ap_comments_progress"
style= "@style/loadingprogress"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"/>
<textview
Android:id= "@+id/tv_loading"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "5DP"
android:text= "@string/loading"
Android:textcolor= "@color/text_color_blue"
Android:textsize= "16sp"/>
<linearlayout
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/linear_layout_btn"
android:visibility= "Visible"
android:orientation= "Vertical" >
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/stop_smart_config"
Android:id= "@+id/btn_cancel_config"
android:onclick= "Cancelconfig"
/>
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/resmart_config"
Android:id= "@+id/btn_re_config"
android:onclick= "Reconfig"
/>
</LinearLayout>
</LinearLayout>
To introduce this layout in a mainactivity, add the following code to the Activity_main.xml:
<include
Android:id= "@+id/layout_loading"
layout= "@layout/layout_progress"/>
Above the Layout_progress.xml, set the visibility property: android:visibility= "Gone", this layout is hidden, I want to click on a button to show this layout and hide the current layout. Write a button in the Activity_main.xml:
Activity_main.xml
<button
Android:id= "@+id/bt_config_router"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_gravity= "left"
android:onclick= "Configtorouter"
android:text= "@string/configure_to_route"/>
This is written in mainactivity:
Private View loadinglayout,bodylayout;//Define reference layout and current layout
Loadinglayout = Findviewbyid (r.id.layout_loading);
Bodylayout = Findviewbyid (r.id.body_layout);
public void Configtorouter (View v) {
Loadinglayout.setvisibility (view.visible);//Display reference layout
Bodylayout.setvisibility (View.gone);//Hide Current layout
}
By clicking the button, the display reference layout is triggered and the current layout is hidden.
Use of include tags in android (open reference layout, hide current layout)