material-design - android在xml中加入FloatingActionButton后程序崩溃的问题?

查看:220
本文介绍了material-design - android在xml中加入FloatingActionButton后程序崩溃的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

问题:
我在布局xml中加入android.support.design.widget.FloatingActionButton,并在Activity中加载该布局后,程序会崩掉。(当然了,不加这个fab的话是不会崩的,说明问题就在fab这里)
于是我用android studio生成带有fab的样例Activity,对比后,发现在AndroidManifest文件中的activity的属性中有一句
android:theme="@style/AppTheme.NoActionBar"
而这个style文件中的AppTheme.NoActionBar对应的内容是这样的:

<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
</style>

于是我也写这么一个AppTheme.NoActionBar,给我写的带有fab的Activity给加上,发现果然不崩了。
奇怪的是,如果我去掉这个style中的两个item,即写成这样:

<style name="AppTheme.NoActionBar">
</style>

结果依然能跑,所以我想请教一下诸位大神,这个android:theme="@style/AppTheme.NoActionBar"在这里是干嘛用的,为什么会导致我上述的问题。

解决方案

我是题主,我找到原因了,加载Floatingactionbutton时需要Activity中的theme的colorAccent属性来确定颜色。当没指明Activity的theme时,用的是默认的theme,是没有colorAccent这个属性的,所以自然会出错。而关于android:theme="@style/AppTheme.NoActionBar"这句,在styles.xml中,AppTheme的定义是:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
</style>

里面指明了colorAccent,所以AppTheme.NoActionBar有了colorAccent的定义,至于其它item,删掉也不影响,所以删掉也能正常加载。

这篇关于material-design - android在xml中加入FloatingActionButton后程序崩溃的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆