如何使用数据绑定设置layout_gravity

时间:2018-04-11 10:27:08

标签: android

我正在尝试使用数据绑定将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.isReceivedtrue时与START对齐,在viewModel.isReceivedfalse时与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的情况下执行此操作吗?

1 个答案:

答案 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>