Android 使用本机小部件膨胀ViewStub
是否可以使用本机android小部件膨胀ViewStub 例如:Android 使用本机小部件膨胀ViewStub,android,android-layout,viewstub,Android,Android Layout,Viewstub,是否可以使用本机android小部件膨胀ViewStub 例如: <LinearLayout...> <TextView...> <TextView...> <ViewStub...> </LinearLayout> 我想用开关或复选框替换/膨胀ViewStub。有可能吗?要将布局设置为充气到视图存根中,请执行以下操作之一: 通过XML设置布局: <ViewStub android:id="@+id/stub"
<LinearLayout...>
<TextView...>
<TextView...>
<ViewStub...>
</LinearLayout>
我想用开关或复选框替换/膨胀ViewStub。有可能吗?要将布局设置为充气到
视图存根中,请执行以下操作之一:
通过XML设置布局:
<ViewStub
android:id="@+id/stub"
android:inflatedId="@+id/myLayout"
android:layout="@layout/myLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
然后,您可以对视图存根进行充气
View inflatedView = viewStub.inflate();
您可以在需要的地方调用此inflate()
方法,例如单击开关
或复选框
(根据您的要求)。目前无法将存根视图
替换为视图
对象。必须创建表示要替换的视图的布局。因此,例如,如果要使用单个复选框切换它,则需要创建如下布局
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_checkbox"
android:text="Click me"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
如果需要膨胀视图的引用,可以改用此引用
CheckBox checkInflated = (CheckBox)viewStub.inflate();
你读过这里吗,你需要的和我一样是的。这与我试图夸大的本机android小部件的布局并不完全相同。我没有一个明确的布局。你应该创建一个新的布局来代表你想要充气的东西。目前无法在StubViews中膨胀视图对象。因此,如果要膨胀本机android switch小部件,应将其设置为什么布局资源?您可以创建一个包含switch小部件的布局文件。然后膨胀布局资源。
viewStub.setLayoutResource(R.layout.my_checkbox);
viewStub.inflate();
CheckBox checkInflated = (CheckBox)viewStub.inflate();