数据绑定中看不到工具栏
Toolbar not seen in 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>
你确定你的内容视图就是这个布局吗?
<include layout="@layout/content_create"
android:id="@+id/contentCreateHolder"
bind:customer="@{customer}"/>
如果没有,删除这一行
createBinding = DataBindingUtil.setContentView(this, R.layout.content_create);
我无法显示 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>
你确定你的内容视图就是这个布局吗?
<include layout="@layout/content_create"
android:id="@+id/contentCreateHolder"
bind:customer="@{customer}"/>
如果没有,删除这一行
createBinding = DataBindingUtil.setContentView(this, R.layout.content_create);