在数据绑定中看不到工具栏

时间:2016-08-12 00:55:55

标签: android android-toolbar android-databinding

我无法让Toolbar显示出来。我做了一切。 我不是Android开发的新手 我用setSupportActionBar() 我尝试调试并查看getSupportActionBar()是否为空。它是空的。
我不知道为什么会这样。自从我开始使用Android DataBinding以来,我一直在体验这一点。

这就是我所做的一切:

binding = DataBindingUtil.setContentView(this, R.layout.activity_create);
createBinding = DataBindingUtil.setContentView(this, R.layout.content_create);

setSupportActionBar(binding.createToolbar);

if (getSupportActionBar() != null) {
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    String action = getIntent().getStringExtra("action");
    if (action != null) {
        if (action.equals("create")) {
            getSupportActionBar().setTitle(R.string.create);
        } else {
            getSupportActionBar().setTitle(R.string.update);
        }
    }
}

我的XML

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:bind="http://schemas.android.com/apk/res-auto">

        <data>
            <import type="java.util.List"/>
            <variable
                name="customer"
                type="co.company.Create.Customer"/>
        </data>

        <android.support.design.widget.CoordinatorLayout
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".CreateActivity">

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

                <android.support.v7.widget.Toolbar
                    android:id="@+id/createToolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@color/colorPrimaryDark"
                    app:popupTheme="@style/AppTheme.PopupOverlay"
                    app:layout_scrollFlags="scroll|enterAlways" />

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

            <include layout="@layout/content_create"
                android:id="@+id/contentCreateHolder"
                bind:customer="@{customer}"/>

        </android.support.design.widget.CoordinatorLayout>
    </layout>

1 个答案:

答案 0 :(得分:2)

您确定要将内容视图设为此布局吗?

<include layout="@layout/content_create"
    android:id="@+id/contentCreateHolder"
    bind:customer="@{customer}"/>

如果没有,请删除此行

createBinding = DataBindingUtil.setContentView(this, R.layout.content_create);