android - 如何在 Kotlin 中以编程方式设置 ShimmerLayout 的布局?
问题描述
注意:我的问题与此不同。
我想ShimmerLayout
在 Kotlin 代码中以编程方式设置布局。那有可能吗?
目前,我可以像这样通过 XML 设置布局:
<com.facebook.shimmer.ShimmerFrameLayout
android:id="@+id/shimmerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="@{showSimmerBanner ? View.VISIBLE : View.GONE, default=gone}">
<LinearLayout
android:id="@+id/shimmerOrientation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/shimmer_banner" />
<include layout="@layout/shimmer_banner" />
<include layout="@layout/shimmer_banner" />
</LinearLayout>
</com.facebook.shimmer.ShimmerFrameLayout>
@layout/shimmer_banner
我们可以从 Kotlin 代码中设置布局 ( ) 吗?
所以,xml是这样的:
<com.facebook.shimmer.ShimmerFrameLayout
android:id="@+id/shimmerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="@{showSimmerBanner ? View.VISIBLE : View.GONE, default=gone}"/>
在 Kotlin 代码中,像这样调用这个 xml: shimmerLayout.set = R.layout.shimmer_banner
,但它没有以编程方式从 Kotlin 代码设置布局的功能。
如何通过 Kotlin 代码仍然有一个LinearLayout
with vertical
+ 可以做到这一点add more 1 <include> tag
?
解决方案
通过<include>
标签包含的布局只是膨胀到父级中,因此您可以简单地使用 LayoutInflater 来做到这一点。
假设(根据您的问题)您对微光布局的引用为shimmerLayout
,您可以这样做:
val inflater = LayoutInflater.from(shimmerLayout.context)
inflater.inflate(R.layout.shimmer_banner, shimmerLayout)
如果您已经有视图,要更改它,您需要执行shimmerLayout.removeAllViews()
,然后执行上述操作。
推荐阅读
- selenium - 在带有 Dameware 和 IE11 的 Windows 10 上运行的 Selenium 找不到任何元素
- azure-active-directory - 从组织内的用户获取 officeLocation
- go - 将结构放入切片时出现golang错误
- java - 无法将名称“X”解析为(n)“类型定义”组件
- dart - 如何使用 testBuilder 测试 source_gen 注释构建器?
- html - 即使调整窗口大小,如何使 div 以关键帧动画为中心
- java - java.util.Random 真的那么随机吗?我怎样才能生成52!(阶乘)可能的序列?
- vbscript - 不同的随机值
- java - Java lamba 参数类型未正确推断
- excel - 在 Excel 2007 中增加文本字符串中的最后一个数字