日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ViewStub使用

發布時間:2023/12/20 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ViewStub使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

一、ViewStub是什么?

<ViewStub>?標簽實質上是一個寬高都為 0 的不可見 的輕量級View。通過延遲按需加載布局的方式提升頁面加載速度。

二、ViewStub使用場景

某布局默認是不可見,當滿足特定場景才顯示。比如網絡異常提示、引導頁等。

三、ViewStub怎么使用?

1、創建布局文件layout_test.xml(注:根標簽可以是布局或控件,但不能為<merge>,子標簽可以使用<merge>

<TextViewxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="40dp"android:text="test"/>

2、通過ViewStub的android:layout指定懶加載的布局layout_test.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:orientation="vertical"><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="測試" /><ViewStubandroid:id="@+id/viewStub"android:layout_width="match_parent"android:layout_height="300dp"android:layout="@layout/layout_test" /> </LinearLayout>

3、顯示/隱藏布局

(1)只在滿足條件進行顯示

//方法1 viewStub.inflate()//方法2 viewStub.visibility = View.VISIBLE

注:ViewStub只能被Inflate一次,inflate之后ViewStub對象會被置空,就不能夠再通過ViewStub來控制顯隱。其中方法2內部也是通過調用inflate,所有ViewStub的setVisibility()和inflate()都只能調用一次

(2)需要根據條件進行顯隱控制(通過ViewStub.inflate()返回的根布局進行顯隱操作)

private var mIsVisiable = falseprivate val mView: View by lazy { viewStub.inflate() }override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)button.setOnClickListener {if (mIsVisiable){mView.visibility = View.INVISIBLE}else{mView.visibility = View.VISIBLE}mIsVisiable = !mIsVisiable}}

?

總結

以上是生活随笔為你收集整理的ViewStub使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。