使用MD support library的时候遇到的一些不理解的报错

使用Toolbar报错

Error inflating class android.support.v7.widget.Toolbar
网上别人的解决办法有:
https://stackoverflow.com/questions/26561235/error-inflating-class-android-support-v7-widget-toolbar
1、rebuild project
2、Go to File -> Invalidate Caches/Restart.

我的经验:
有可能是设置了logo的原因

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/design_default_color_primary"
        app:title="Toolbar"
        app:logo="@drawable/ic_launcher_foreground"
        app:navigationIcon="@drawable/ic_list_black_24dp"
        app:subtitle="导航 logo title view action"
        android:theme="@style/Base.V7.Widget.AppCompat.Toolbar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        >
    </android.support.v7.widget.Toolbar>

我把logo删除就正常显示了

显示logo:https://stackoverflow.com/questions/30851299/set-toolbar-logo-in-xml
就是 想在上面显示logo咋办呢? 虽然需求很少,那就是放一个子布局,里面用imageView

navigation 和 action依然会保存下来
在这里插入图片描述

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="@color/design_default_color_primary"
        app:navigationIcon="@drawable/ic_list_black_24dp"
        android:theme="@style/Base.V7.Widget.AppCompat.Toolbar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/media_image"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="3"
                android:adjustViewBounds="true"
                android:contentDescription="new post image"
                android:scaleType="centerCrop"
                android:src="@drawable/sakura"
                android:visibility="visible" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:text="装作是个title"
                />

            <TextView
                android:id="@+id/toolbar_subtitle"
                android:text="????"
                android:textSize="14sp"
                android:layout_gravity="center"
                android:textColor="@color/colorPrimary"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                />
        </LinearLayout>

    </android.support.v7.widget.Toolbar>

使用appBar报错 【Could not inflate Behavior】

我真的一万头草泥马
Could not inflate Behavior subclass android.support.design.widget.appbar_scrolling_view_behavior
就是使用资源文件报错,直接输入string就行…
参考:https://codeday.me/bug/20180203/126250.html
https://stackoverflow.com/questions/30562330/using-appcompat-layout-behavior-with-string-appbar-scrolling-view-behavior

另外:关于标题,我直接只复制了xml的时候,标题在全部展开的时候是没有隐藏的,但是在我找的sample中是只有折叠图片你的时候,才显示标题的
这涉及到判断appBarLayout的展开位置,然后setTitle就可以了。附上sample这部分的代码

        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                Log.e("zhouwei","appbarHeight:"+appBarLayout.getHeight()+
                	" getTotalScrollRange:"+appBarLayout.getTotalScrollRange()+" offSet:"+verticalOffset);
                if(Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()){
                    toolbar.setTitleTextColor(getResources().getColor(R.color.white));
                    collapsingToolbarLayout.setTitle("AppbarLayout");
                }else{
                    collapsingToolbarLayout.setTitle("");
                }
            }
        });

然后附上我最后的XML

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

                   <!--<android.support.v7.widget.Toolbar-->
                       <!--android:layout_width="match_parent"-->
                       <!--android:layout_height="200dp"-->
                       <!--android:minHeight="?attr/actionBarSize"-->
                       <!--app:title="AppbarLayout"-->
                       <!--android:gravity="bottom"-->
                       <!--app:titleTextColor="@color/white"-->
                       <!--app:layout_scrollFlags="scroll|snap"-->
                       <!--/>-->
    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapse_layout"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        app:contentScrim="@color/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/sakura"
            app:layout_collapseMode="parallax"
            />
        <android.support.v7.widget.Toolbar
            android:id="@+id/appbar_layout_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:title="AppbarLayout"
            app:titleTextColor="@color/white"
            app:navigationIcon="@mipmap/ic_launcher_round"
            app:layout_collapseMode="pin"
            />

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

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

这一部分,下一步学习一下怎么把图片完全拉开,然后自动缩回固定的位置
还有,为什么别人的wifi标识的那部分颜色也变了呢,而我的颜色依然是程序的颜色

Chip 报错

Error inflating class android.support.design.chip.Chip
参考:https://stackoverflow.com/questions/51877777/android-material-chip-component-crashing-app-unable-to-inflate-xml/51878880

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值