Android: AAPT: error: duplicate attribute.

Android: AAPT: error: duplicate attribute.

在使用 Android 的数据绑定时, 需要使用 <layout> 标签来包裹这个布局才能生成对应的数据绑定对象。

出现问题的 XML 文件:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <data>
        <variable
            name="bookEntity"
            type="com.example.bookrecommend.entity.BookEntity" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:contentDescription="@string/book_image_string"
            app:srcCompat="@{bookEntity.imageUrl}" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="8dp"
            android:orientation="vertical">

            <TextView
                android:textAlignment="textStart"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2.5"
                android:text="@{bookEntity.bookName}"/>
            
            <TextView
                android:textAlignment="viewEnd"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1.5"
                android:text="@{bookEntity.authorsString}"/>

        </LinearLayout>
    </LinearLayout>
</layout>

在重新构建这个项目时会出现错误:AAPT: error: duplicate attribute. 大概意思就是重复声明了相同的属性。实际上,<layout> 标签是不需要 android:layout_widthandroid:layout_height 属性的,因此会提示重复设置属性的错误。

解决方案:移除 <layout> 中多余的这两个属性即可。
解决后的代码如下:

<?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>
        <variable
            name="bookEntity"
            type="com.example.bookrecommend.entity.BookEntity" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:contentDescription="@string/book_image_string"
            app:srcCompat="@{bookEntity.imageUrl}" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="8dp"
            android:orientation="vertical">

            <TextView
                android:textAlignment="textStart"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2.5"
                android:text="@{bookEntity.bookName}"/>
            
            <TextView
                android:textAlignment="viewEnd"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1.5"
                android:text="@{bookEntity.authorsString}"/>

        </LinearLayout>
    </LinearLayout>
</layout>

猜你喜欢

转载自blog.csdn.net/FatalFlower/article/details/113884010
今日推荐