【问题标题】:Unable to show toolbar while using databinding in Android在 Android 中使用数据绑定时无法显示工具栏
【发布时间】:2017-05-04 09:24:56
【问题描述】:

我正在尝试在活动中显示工具栏和菜单项。这工作正常,直到我将数据绑定添加到 xml。在那之后我根本不能使用 setSupportActionBar(toolbar) 。这是我的 xml 文件 activity_single_product.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
    <variable name="product" 
       type="com.sales.models.ProductModel"/>
    <variable name="glide" type="com.bumptech.glide.Glide" />
</data>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_view_single_product"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sales.SingleProductActivity"
    android:background="@color/colorPrimary">
    <LinearLayout
        android:id="@+id/container_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            android:id="@+id/tool"
            layout="@layout/toolbar" />

        <include
            android:id="@+id/search_layout"
            layout="@layout/search_layout" />
        <View
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:background="@drawable/drop_shadow_toolbar" />
    </LinearLayout> <!-- Toolbar & Search bar-->
</RelativeLayout>
</layout>

活动:

private void initViews() {
    binding = DataBindingUtil.setContentView(this, R.layout.activity_single_product); // ActivitySingleProductBinding
    setSupportActionBar(binding.tool.toolbar);
    binding.setProduct(product);
}

我已关注stackoverflow answer。这没有帮助。任何人都可以建议吗?

【问题讨论】:

  • 请有意义的 cmets 否决投票!

标签: android xml android-layout data-binding


【解决方案1】:

我发现并解决了这个问题。问题出在工具栏布局中。我包括了

<include
        android:id="@+id/tool"
        layout="@layout/toolbar" />

工具栏布局是

<android.support.v7.widget.Toolbar 
    xmlns:local="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:layout_marginTop="25dp"
    android:background="@null"
    android:titleTextColor="@color/lightGrey2"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:contentInsetLeft="10dp"
    android:contentInsetRight="10dp"
    android:contentInsetStart="10dp"
    android:padding="0dp"
    app:contentInsetLeft="10dp"
    app:contentInsetRight="10dp"
    app:contentInsetStart="10dp"/>

问题是,我没有包含适当的标签来包装工具栏。后来我改了,新的工具栏布局是

<layout 
    xmlns:local="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools">
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:layout_marginTop="25dp"
    android:background="@null"
    android:titleTextColor="@color/lightGrey2"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:contentInsetLeft="10dp"
    android:contentInsetRight="10dp"
    android:contentInsetStart="10dp"
    android:padding="0dp"
    app:contentInsetLeft="10dp"
    app:contentInsetRight="10dp"
    app:contentInsetStart="10dp"/></layout>

这解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多