Android dataBinding-如何使用布尔资源触发布局的可见性 [英] Android dataBinding - how to use bool resource to trigger visibility of layout

查看:401
本文介绍了Android dataBinding-如何使用布尔资源触发布局的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在android中有一个bool.xml文件,如下所示:

I currently have a bool.xml file in android which looks like this:

   <?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="showAds">true</bool>
    </resources>

现在我有一个使用数据绑定的layout.xml文件。我想基于上面定义的布尔showAds显示或隐藏adView的可见性。到目前为止,我有这个:

Now i have a layout.xml file which uses databinding. I want to show or hide the visilibity of a adView based on a the boolean showAds defined above. So far i have this:

  <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
android:visibility="@{@bool/showAds ? View.Visible:View:gone}"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-1234567/34343">

,但它不会编译。如何获取布尔值以决定广告是否应展示?语法错误。

but it does not compile. how can i get the boolean to decide if the ad should show or not ?The syntax is wrong.

推荐答案

更正条件View.Visible:View:gone

Correct the syntax of condition View.Visible:View:gone

android:visibility="@{@bool/showAds ? View.VISIBLE:View.GONE}"

,您需要在数据部分中导入查看:

and you need to import View in the data section:

<data>
    <import type="android.view.View"/>
</data>

这篇关于Android dataBinding-如何使用布尔资源触发布局的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆