kotlin 使用viewStub
ViewStub
是一个轻量级的的View,继承于ViewGroup,没有任何尺寸,不绘制任何东西,因此绘制或者移除时更省时。(ViewStub不可见,大小为0)
优点
实现View的延迟加载,避免资源的浪费,减少渲染时间,在需要的时候才加载View
缺点
- ViewStub所要替代的layout文件中不能有标签
- ViewStub在加载完后会被移除,或者说是被加载进来的layout替换掉了
用法
<ViewStubandroid:id="@+id/stub_id"android:layout="@layout/view_stub_layout"android:layout_width="match_parent"android:layout_height="wrap_content" />
view_stub_layout.xml 文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/index_page_bg"android:orientation="vertical"><TextViewandroid:id="@+id/tv_advert_hot_expert"android:layout_width="match_parent"android:layout_height="100dp"android:layout_marginTop="5dp"android:layout_marginBottom="5dp"android:background="@color/white"android:gravity="center"android:text="预留广告位置" /></LinearLayout>
用ViewStub加载layout文件时,可以调用 setVisibility(View.VISIBLE)
或者inflate()
- stub_id.inflate() 或者
- stub_id.visibility = View.VISIBLE
注意
- 一旦ViewStub visible/inflated,则ViewStub将从视图框架中移除,其id
stub_import
也会失效 - ViewStub被绘制完成的layout文件取代,并且该layout文件的root view的id是
android:inflatedId
指定的idpanel_import
,root view的布局和ViewStub视图的布局保持一致
所以,当inflate()之后,需要这样写:
tv_advert_hot_expert!!.text = "我是viewstub里的view"
stub_id已经失效,如果再使用这个id,会报nullpointException;
kotlin 使用viewStub相关推荐
- Android最全面试题整理、还有Kotlin
目录 1.理解线程间通信 2.工作者线程(workerThread)与主线程(UI线程)的理解 3.通过Handler在线程间通信的原理 4.子线程发消息到主线程进行更新 UI,除了 handler ...
- springboot实现SSE服务端主动向客户端推送数据,java服务端向客户端推送数据,kotlin模拟客户端向服务端推送数据
SSE服务端推送 服务器向浏览器推送信息,除了 WebSocket,还有一种方法:Server-Sent Events(以下简称 SSE).本文介绍它的用法. 在很多业务场景中,会涉及到服务端向客户端 ...
- 在kotlin companion object中读取Bean,注入Bean对象
在kotlin companion object中读取Bean,注入Bean对象 在使用kotlin时,或多或少地会使用到一些公共组件,如 http. mongo. redis相关的组件. 使用组 ...
- 在kotlin companion object中读取spring boot配置文件,静态类使用@Value注解配置
在kotlin companion object中读取配置文件 静态类使用@Value注解配置 class Config {@Value("\${name}")fun setNam ...
- kotlin中继承父属性使用构造方法
kotlin中继承父属性使用构造方法 1. 定义父类,给它两个属性: abstract class AbstractResponseMessage {private var success = tru ...
- Java | kotlin 手动注入bean,解决lateinit property loginService has not been initialized异常
kotlin.UninitializedPropertyAccessException: lateinit property loginService has not been initialized ...
- SpringBoot项目使用nacos,kotlin使用nacos,java项目使用nacos,gradle项目使用nacos,maven项目使用nacos
SpringBoot项目使用nacos kotlin demo见Gitte 一.引入依赖 提示:这里推荐使用2.2.3版本,springboot与nacos的依赖需要版本相同,否则会报错. maven ...
- OpenAPI使用(swagger3),Kotlin使用swagger3,Java使用swagger3,gradle、Maven使用swagger3
OpenAPI使用(swagger3) demo见Gitte 一.背景及名词解释 OpenAPI是规范的正式名称.规范的开发工作于2015年启动,当时SmartBear(负责Swagger工具开发的公 ...
- Gradle 将项目publish到Nexus,Kotlin将项目发布到nexus,springboot项目发布到maven仓库
示例见:Gitte 公仓设置 在项目中添加maven-publish的插件 plugins {kotlin("jvm") version "1.3.72"kot ...
- kotlin Bean加载失败lateinit property has not been initialized
Application run failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creat ...
最新文章
- 外行人都能看懂的SpringCloud,错过了血亏!
- 【题解】P4124 [CQOI2016]手机号码
- 资讯|WebRTC M95 更新
- 解决wps与matlab不兼容的问题
- 解决 【xshell 5 不能使用退格键和Delete建】的问题
- web前端 真是一条不归路,2年前的文章 如今依然影响很多人
- 用SpriteBuilder简化quot;耕牛遍地走quot;的动画效果(二)
- 数学建模中常用的方法
- MySQL集群:高可用性DBMS
- Java中的equals和==的差别 以及Java中等价性和同一性的讨论
- Linux网络协议栈:一个TCP链接的耗时
- MFC 改变控件字体大小
- 条件测试 linux,Linux的条件测试
- php srem,Redis Srem 命令
- 关于android studio报错Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.
- python熊猫弹幕_python奇闻杂技05 爬虫初步学习+jieba分词+词云库+哔哩哔哩弹幕爬取示例(数据分析pandas)...
- Leetcode 1925. Count Square Sum Triples [Python]
- Android 图片文件读取
- 初识html5使用jsQR识别二维码
- SessionFactory在Hibernate中的作用