我使用了一个包含布局,我需要更改它的可见性:
<include
android:id="@+id/layout_provinces"
layout="@layout/layout_select_provinces"
/>layout_select_provinces是这样的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/select_province"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:visibility="gone">
<LinearLayout
android:id="@+id/layout_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_color"
android:orientation="vertical">
<TextView
android:id="@+id/txt_state"
style="@style/FontIranBold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/_8sdp"
android:layout_marginRight="@dimen/_4sdp"
android:layout_marginBottom="@dimen/_8sdp"
android:text="@string/txt_select_province"
android:textSize="@dimen/font_size_small" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/_1sdp"
android:background="@color/gray_special" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_estate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layout_top"
android:layout_marginTop="@dimen/_8sdp" />
但是,当我将Id设置为RelativeLayout时,我的应用程序会崩溃,并且无法更改可见性:
binding.layoutProvinces.selectProvince.setVisibility(View.GONE);有人能帮我处理ViewBinding设置id过程吗?
发布于 2020-04-29 05:43:31
当我们给<include>一个id时,它会覆盖我们所包含的根布局的id。在您的例子中,layout_provinces正在覆盖<RelativeLayout的select_province,所以当您访问相对布局时,它会给您一个错误,因为它已经不在了。
您可以做的是移除相对布局的id,只提供id来包含和访问通过getRoot()方法包含的根布局,如下所示。
Java
binding.selectProvince.getRoot().setVisibility(View.GONE)Kotlin
binding.selectProvince.root.visibility = View.GONE视图绑定中的getRoot()方法返回给定布局的最顶层视图(在您的情况下是RelativeLayout )。
发布于 2020-04-27 16:36:42
要使DataBinding工作,需要将<layout>标记作为根标记。因此,将xml代码包装在<layout>中如下所示:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id="@+id/select_province"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:visibility="gone">
<LinearLayout
android:id="@+id/layout_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_color"
android:orientation="vertical">
<TextView
android:id="@+id/txt_state"
style="@style/FontIranBold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/_8sdp"
android:layout_marginRight="@dimen/_4sdp"
android:layout_marginBottom="@dimen/_8sdp"
android:text="@string/txt_select_province"
android:textSize="@dimen/font_size_small" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/_1sdp"
android:background="@color/gray_special" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_estate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layout_top"
android:layout_marginTop="@dimen/_8sdp" />
</RelativeLayout>
</layout>https://stackoverflow.com/questions/61453179
复制相似问题