Use of include tags in android (open reference layout, hide current layout)

Source: Internet
Author: User

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)

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.