Android 使用数据绑定时如何在横向模式下隐藏按钮
我有一个视图,它使用数据绑定来显示ViewModel中的数据。当视图处于横向模式时,视图有一个隐藏的按钮。目前,有两个LayoutFile就可以了,但我只想有一个,因为重构是一场噩梦。但是如何使用数据绑定和绑定适配器来实现这一点呢 如何为给定视图启用以下bindingexpression android:visibility=@{isLandscapeMode?View.INVISIBLE:View.VISIBLE} 编辑: 我的扩展属性定义ViewUtils.kt: 和我的布局:Android 使用数据绑定时如何在横向模式下隐藏按钮,android,android-databinding,Android,Android Databinding,我有一个视图,它使用数据绑定来显示ViewModel中的数据。当视图处于横向模式时,视图有一个隐藏的按钮。目前,有两个LayoutFile就可以了,但我只想有一个,因为重构是一场噩梦。但是如何使用数据绑定和绑定适配器来实现这一点呢 如何为给定视图启用以下bindingexpression android:visibility=@{isLandscapeMode?View.INVISIBLE:View.VISIBLE} 编辑: 我的扩展属性定义ViewUtils.kt: 和我的布局: <la
<layout 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">
<data>
<import type="android.view.View"/>
<import type="com.example.base.views.ViewUtilsKt" />
<variable
name="isLandscapeMode "
type="java.lang.Boolean"/>
<variable
name="viewmodel"
type="com.example.ui.task.TaskViewModel" />
</data>
...
<ImageButton
...
android:visibility="@{isLandscapeMode ? View.INVISIBLE : View.VISIBLE}"
...
/>
这会导致编译错误:原因:无效名称:isLandscapeMode您必须在绑定适配器中检查当前打开的电话方向。这是另一篇你可以找到你问题答案的帖子 编辑: 您需要检测方向并将其保存为布尔值。稍后,您必须将该变量传递给适配器,在本例中,适配器将是布尔型的
<data>
<import type="android.view.View"/>
<variable
name="isLandscapeMode"
type="boolean"/>
</data>
对于其他人,如果他们需要相同的行为。有两种解决办法 第一个解决方案创建BindingAdapter并在您选择的UI元素上使用它:
@BindingAdapter("bind:visibleInLandscapeMode")
fun View.setVisibleInLandscapeMode(visible: Boolean) {
Timber.d("setVisibleInLandscapeMode() returns ${resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE}")
visibility = if (visible && (resources.configuration.orientation != Configuration.ORIENTATION_LANDSCAPE)) VISIBLE else INVISIBLE
}
在XML布局中:
<layout 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"
xmlns:bind="http://schemas.android.com/apk/lib/com.example">
...
<ImageButton
...
bind:visibleInLandscapeMode="false"
...
/>
第二种解决方案创建一个bindingexpression,用于更改UI元素的可见性:
<ImageButton
...
android:visibility="@{context.getResources.getConfiguration.orientation == Configuration.ORIENTATION_LANDSCAPE ? View.INVISIBLE : View.VISIBLE}"
...
/>
记住正确的导入:
<layout 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">
<data>
<import type="android.view.View"/>
<import type="android.content.res.Resources" />
<import type="android.content.res.Configuration" />
...
</data>
...
顺便说一句,使用数据绑定时要小心。请参阅问题和。我知道如何检测方向。问题涉及如何启用bindingexpression。你怎么能做到android:visibility=@{isLandscapeMode?View.INVISIBLE:View.VISIBLE}你应该在最初的问题中包含这些信息。我已经编辑了我的回答,如果视图能够正确呈现的话。它一直显示我试图隐藏的按钮:问题出在哪里?@Khemraj您在哪里/如何创建isLandscapeMode函数以便能够在上面的bindingexpression中使用它?您可以在ViewModel类中使用它。是否可以在View上使用扩展函数?我在用科特林。我不想把它放在我的ViewModel中。当然,你可以把它放在Util类中。并以类似-Util.isLandscapeMode的布局访问它。
<layout 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">
<data>
<import type="android.view.View"/>
<import type="android.content.res.Resources" />
<import type="android.content.res.Configuration" />
...
</data>
...