如何使用Android DataBinding动态更改android:layout属性? [英] How can I use Android DataBinding to dynamically change android:layout property?

查看:736
本文介绍了如何使用Android DataBinding动态更改android:layout属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 XML 布局代码:

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:bind="http://schemas.android.com/apk/res-auto">
    <data>
        <variable name="info" type="com.mycompany.orm.binders.MyBinderObject"/>
    </data>

    <android.support.percent.PercentRelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        android:gravity="end">

    ...

这很好用.但是,我在MyBinderObject中有一个称为mygravity的属性,并将其设置为以下两个字符串之一:startend.我试图弄清楚如何读取数据绑定属性mygravity以将重力分配给布局.

And this works great. However, I have a property inside MyBinderObject called mygravity, and it is set to one of two strings: start or end. I am trying to figure out how to read the Data Binding property mygravity to assign the gravity to the layout.

android:gravity="@{info.mygravity}"(但是,此行不起作用.编译时数据绑定代码失败).

I.e., android:gravity="@{info.mygravity}" (However, this line does not work. The compile-time databinding code fails).

有人知道如何基于数据绑定对象"属性动态设置android:gravity吗?

Does anyone have an idea how to dynamically set the android:gravity based on a Data-bound objects property?

推荐答案

我的MyBinderObject内部有一个名为mygravity的属性,它设置为两个字符串之一:start或end.

I have a property inside MyBinderObject called mygravity, and it is set to one of two strings: start or end.

其中存在一个问题:您正在做一些合理的事情,并将布局XML像XML一样对待.

Therein lies the problem: you're doing something reasonable and treating the layout XML like XML.

布局XML属性值可以具有多种类型.有时,这很明显,例如android:enabled="false"android:layout_width="30sp".而且,有时类型实际上是字符串或字符串资源引用(例如,android:text="Foo").但是有时候属性值看起来像一个英语字符串,但实际上它是一组枚举值(例如,重力值)中的一个.

The layout XML attribute values can be of a variety of types. Sometimes, this is obvious, such as android:enabled="false" or android:layout_width="30sp". And, sometimes, the type really is a string or string resource reference (e.g., android:text="Foo"). But sometimes the attribute value looks like an English-language string, but it is really one of a set of enumerated values (e.g., gravity values).

我没有意识到重力实际上是一个枚举的属性

I didn't realize that gravity was actually an enumerated attribute

一个粗略的经验法则:如果将值转换为另一种语言后构建会失败,则它是一个枚举的属性(例如,android:gravity="fin"而不是android:gravity="end",假设Google Translate给我合理的条件)那里是西班牙文...).如果转换后的值可以正常工作,则它是任何有效的字符串或(通常)字符串资源.

A rough-cut rule of thumb: if the build would fail if you translated the value into another language, then it is an enumerated attribute (e.g., android:gravity="fin" instead of android:gravity="end", assuming that Google Translate gave me reasonable Spanish there...). If the translated value would work just fine, then it's any valid string or (usually) string resource.

我试图弄清楚如何读取数据绑定属性mygravity来将重力分配给布局.

I am trying to figure out how to read the Data Binding property mygravity to assign the gravity to the layout.

您将不得不将字符串值转换为等效的Gravity常量,例如Gravity.START.

You will have to transmogrify the string values into equivalent Gravity constants like Gravity.START.

这篇关于如何使用Android DataBinding动态更改android:layout属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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