我正在尝试使用数据绑定将layout_gravity
设置为LinearLayout
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.Gravity" />
<variable
name="viewModel"
type="com.example.viewModel.MessageViewModel" />
</data>
<LinearLayout
android:id="@+id/messageAdapter_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="@{viewModel.isReceived ? Gravity.START : Gravity.END}"
android:orientation="vertical"
app:setBackground="@{viewModel.isReceived}">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{viewModel.content}"
android:textColor="@color/primaryTextColorInv" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="@color/secondaryTextColorInv"
android:textStyle="italic" />
</LinearLayout>
</LinearLayout>
</layout>
嵌套的LinearLayout应该在viewModel.isReceived
为true
时与START对齐,在viewModel.isReceived
为false
时与END对齐但我收到此错误:
Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'android:layout_gravity' with parameter type int on android.widget.LinearLayout.
可以在不使用@BindingAdapter
的情况下执行此操作吗?
答案 0 :(得分:5)
我找到解决方案设置gravity
到根LinearLayout而不是将layout_grarvity
设置为嵌套的<:p>
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.Gravity" />
<variable
name="viewModel"
type="com.example.viewModel.MessageViewModel" />
</data>
<LinearLayout
android:id="@+id/messageAdapter_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="@{viewModel.isReceived ? Gravity.START : Gravity.END}"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:setBackground="@{viewModel.isReceived}"
app:setMargin="@{viewModel.isReceived}">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{viewModel.content}"
android:textColor="@color/primaryTextColorInv" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="@color/secondaryTextColorInv"
android:textStyle="italic" />
</LinearLayout>
</LinearLayout>
</layout>