没有膨胀,数据绑定无法正常工作

问题描述 投票:0回答:1

我使用以下代码在导航标题中显示设备型号和品牌。膨胀的布局但文字未显示。我在其他一些活动中尝试了相同的代码,我正在使用数据绑定,它工作正常。

我不确定是否有必要使用DataBindingUtil.inflate,但在我的情况下,此视图将用作NavigationBarheader。我已经在我的活动中验证了生成了NavigationViewHeaderBinding类,所以理想情况下它应该可以工作。我可能错了。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
    <import type="android.os.Build" />
</data>

<LinearLayout
    android:id="@+id/ll_nav_header_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingTop="80dp"
    android:paddingRight="20dp"
    android:paddingBottom="10dp">


    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@{Build.BRAND}"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="@color/colorWhite"
        android:textSize="18sp" />

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:text="@{Build.MODEL}"
        android:textAlignment="center"
        android:textColor="@color/colorWhite"
        android:textSize="14sp" />
</LinearLayout>

这是主要的XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".home.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/activity_main" />

<com.andremion.floatingnavigationview.FloatingNavigationView
    android:id="@+id/floating_navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    app:backgroundTint="@color/colorAccent"
    app:drawMenuBelowFab="false"
    app:headerLayout="@layout/navigation_view_header"
    app:layout_anchor="@id/toolbar"
    app:layout_anchorGravity="bottom|end"
    app:menu="@menu/drawer_menu" />

 </android.support.design.widget.CoordinatorLayout>
android android-databinding
1个回答
0
投票

如果你想要你的绑定工作,那么你必须通过数据绑定方式设置你的内容,这必须是以下之一 -

活动 -

DataBindingUtil.setContentView(this, R.layout.activity_main);

对于碎片,对话框/列表 -

ListItemBinding binding = ListItemBinding.inflate(layoutInflater, viewGroup, false);
// or
ListItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false);

来自Android Documentation

此类包含布局属性(例如,用户变量)到布局视图的所有绑定,并知道如何为绑定表达式指定值。

因此,您必须通过DataBinding方式设置内容以使XML变量起作用。否则生成的绑定类将不被使用。

在您的情况下,如果子布局是绑定布局然后让子布局工作,父必须也是绑定布局,并使用绑定类进行膨胀。

© www.soinside.com 2019 - 2024. All rights reserved.