0

我将带有工具栏的 AppBarLayout 放在一个单独的文件中,并将其包含在我的活动布局中。在某些屏幕上,布局需要在字段contentInsetStartcontentInsetStartWithNavigation.

领域contentInsetStartcontentInsetStartWithNavigation甚至不建立。

我如何包含 AppBar 并设置自定义属性

<include
    android:id="@+id/include_app_bar_layout"
    layout="@layout/view_app_bar_layout"
    app:toolbarTitle="@{@string/custom_title}"
    app:toolbarContentInsetStart="@{@dimen/customInsetStart}"
    app:toolbarCISWN="@{@dimen/customInsetStartWithNavigation}"/>

属性是 dp 值

<dimen name="customInsetStart">92dp</dimen>
<dimen name="customInsetStartWithNavigation">92dp</dimen>

带有工具栏和一些变量的 AppBarLayout

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="toolbarTitle"
            type="String" />

        <variable
            name="toolbarContentInsetStart"
            type="java.lang.Float" />

        <variable
            name="toolbarCISWN"
            type="Integer" />

        <variable
            name="appBarLayoutElevation"
            type="java.lang.Float" />

        <variable
            name="appBarLayoutBackground"
            type="Integer" />

    </data>

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@{appBarLayoutBackground}"
        app:elevation="@{appBarLayoutElevation}"
        tools:ignore="RtlHardcoded,RtlSymmetry">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:theme="@style/ThemeOverlay.MyStyle.Toolbar"
            app:title="@{toolbarTitle}"
            app:contentInsetStart="@{toolbarContentInsetStart}"
            app:contentInsetStartWithNavigation="@{toolbarCISWN}"
            />

    </com.google.android.material.appbar.AppBarLayout>
</layout>

问题

toolbarCISWN如果我像上面那样定义变量Integer,那么我会收到一个构建错误,上面写着

找不到接受参数类型“float”的 MyActivityLayoutFileBinding app:toolbarContentInsetStartWithNavigation 的设置器。

如果我将变量类型设置为Floatjava.lang.Float(如海拔变量),那么我会在 xml 文件中app:contentInsetStartWithNavigation得到不是 Float 类型的错误

找不到接受参数类型“java.lang.Float”的 androidx.appcompat.widget.Toolbar app:contentInsetStartWithNavigation 的设置器

4

0 回答 0