注意:我的问题不同于this。
我想用Kotlin代码以编程方式设置ShimmerLayout
的布局。可以这样做吗?
目前,我可以像这样通过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>
我们可以从Kotlin代码中设置布局(@layout/shimmer_banner
)吗?
因此,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
+ vertical
+ can add more 1 <include> tag
的
发布于 2020-03-11 05:03:35
通过<include>
标记包含的布局只会被充气到父级,所以您可以使用LayoutInflater来完成它。
假设(从您的问题中)将shimmer布局引用为shimmerLayout
,您可以这样做:
val inflater = LayoutInflater.from(shimmerLayout.context)
inflater.inflate(R.layout.shimmer_banner, shimmerLayout)
若要更改它,如果已经有视图,则需要执行shimmerLayout.removeAllViews()
,然后执行上面的操作。
https://stackoverflow.com/questions/60629295
复制相似问题