我正在使用数据绑定,我试图编写一些设置。我有一个视图,我希望通过自定义属性(传入枚举)确定其布局。例如,传入Setting.CHECKBOX
或Setting.SWITCH
应该让我为视图充气。视图将定义如下:
<data>
<import type="com.mypackage.ui.SettingView.SettingControl" />
<SettingView
android:id="@+id/setting_foo
android:layout_width="match_parent"
android:layout_height="wrap_content"
setting:control="@{Setting.CHECKBOX} />
...
有没有办法在布局膨胀之前读取setting:control
属性?根据设置,我想指定如何给SettingView充气。
我知道我可以通过自定义属性/样式来实现,但我想知道是否可以通过数据绑定来实现。
答案 0 :(得分:0)
No. In order to look at the attribute value during inflation, you can't use data binding syntax. Data binding will strip the value from the XML and add it to the generated binding code.
Instead, add the attribute to the attrs.xml file and assign the value using the normal syntax. You can then read the attribute using context.obtainedStyledAttributes()
and accessing the value through the TypedArray
.