Android中include標籤的使用(開啟引用布局,隱藏當前布局)

來源:互聯網
上載者:User

標籤:

在開發app的時候,有時候一個布局會反覆用到,可以把反覆用到的布局單獨寫一個xml檔案,什麼時候用到就用includ標籤引入xml

下面是我寫的反覆用到的一個xml,裡面有2個button,一個TextView和一個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>

 

在一個MainActivity引入這個布局,在activity_main.xml加入下面的代碼:

<include
android:id="@+id/layout_loading"
layout="@layout/layout_progress" />

上面的layout_progress.xml中,設定visibility屬性:android:visibility="gone" ,這個布局是隱藏的,我想在點擊一個button的時候顯示這個布局並隱藏當前布局。activity_main.xml中寫一個button:

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" />

在MainActivity中是這麼寫的:

private View loadingLayout,bodyLayout;//定義引用布局和當前布局

  loadingLayout = findViewById(R.id.layout_loading);
  bodyLayout = findViewById(R.id.body_layout);

  public void configToRouter(View v){

    loadingLayout.setVisibility(View.VISIBLE);//顯示引用布局
    bodyLayout.setVisibility(View.GONE);//隱藏當前布局
}

這樣點擊按鈕,就觸發了顯示引用布局,隱藏當前布局。

 

Android中include標籤的使用(開啟引用布局,隱藏當前布局)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.