android实现常驻通知栏遇到的问题及解决办法

 更新时间:2020年06月30日 16:49:16   作者:唐人小调  
这篇文章主要介绍了android实现常驻通知栏遇到的问题及解决办法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

实现常驻通知栏时遇到的问题:

无论如何就是不显示通知,查看日志发现貌似报错了:

2020-06-28 14:11:34.923 6387-6387/xxx E/CrashReport: android.app.RemoteServiceException: Bad notification posted from package xxx: Couldn't inflate contentViewsandroid.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1944)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:192)
        at android.app.ActivityThread.main(ActivityThread.java:6815)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:818)

说的是布局报错,所使用的布局如下:

 根据报错信息来看,就是这个

android.support.constraint.ConstraintLayout

的问题了。

然后将布局的根view修改为RelativeLayout。

运行,报错,,,,纳尼?

2020-06-28 14:24:02.622 11436-11436/xxx E/CrashReport: android.app.RemoteServiceException: Bad notification posted from package xxx: Couldn't inflate contentViewsandroid.view.InflateException: Binary XML file line #2: Binary XML file line #2: You must supply a layout_height attribute.
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1944)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:192)
        at android.app.ActivityThread.main(ActivityThread.java:6815)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:818)

虽然继续报错,但是发现跟第一次报的错不同了,说明第一个报错问题解决。

那么来细看第二个报错信息:

Binary XML file line #2: You must supply a layout_height attribute.

看信息是说布局中缺少layout_height属性,但是确认布局中设置了这属性啊。。。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="@dimen/dp_74"
  android:background="@drawable/shape_bg_resident_notify">
 
 <ImageView
   android:id="@+id/iv_resident_weather"
   android:layout_width="@dimen/dp_45"
   android:layout_height="@dimen/dp_45"
   android:layout_marginStart="@dimen/dp_10"
   android:background="@mipmap/weather_icon_blue_big_cloudy"
   android:layout_centerVertical="true"/>
 
 <TextView
   android:id="@+id/tv_resident_weather_temp"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="28"
   android:textSize="@dimen/sp_40"
   android:textColor="#ff333333"
   android:layout_toEndOf="@+id/iv_resident_weather"
   android:layout_centerVertical="true"
   android:layout_marginStart="@dimen/dp_5"/>
 
 <TextView
   android:id="@+id/tv_resident_degree"
   android:layout_width="@dimen/dp_6"
   android:layout_height="@dimen/dp_6"
   android:layout_marginStart="@dimen/dp_3"
   android:layout_marginTop="@dimen/dp_24"
   android:layout_toEndOf="@+id/tv_resident_weather_temp"
   android:background="@drawable/shape_resident_weather_temp"/>
 
 <TextView
   android:id="@+id/tv_resident_weather_cond"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="多云转晴"
   android:textSize="@dimen/sp_16"
   android:textColor="@color/color_333333"
   android:layout_marginTop="@dimen/dp_14"
   android:layout_marginStart="@dimen/dp_6"
   android:layout_toEndOf="@+id/tv_resident_degree" />
 
 <TextView
   android:id="@+id/tv_resident_temp_range"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="21~34℃"
   android:textColor="@color/color_333333"
   android:textSize="@dimen/sp_16"
   android:layout_marginTop="@dimen/dp_5"
   android:layout_below="@+id/tv_resident_weather_cond"
   android:layout_toEndOf="@+id/tv_resident_degree"
   android:layout_marginStart="@dimen/dp_6"/>
 
 <TextView
   android:id="@+id/tv_resident_aqi"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="空气优"
   android:textSize="@dimen/sp_16"
   android:textColor="@color/color_333333"
   android:layout_alignParentEnd="true"
   android:layout_marginTop="14dp"
   android:layout_marginEnd="@dimen/dp_10"/>
 
 <ImageView
   android:id="@+id/iv_resident_aqi"
   android:layout_width="@dimen/dp_18"
   android:layout_height="@dimen/dp_18"
   android:src="@drawable/ic_icon_aqi"
   android:layout_toStartOf="@+id/tv_resident_aqi"
   android:layout_marginEnd="@dimen/dp_5"
   android:layout_marginTop="@dimen/dp_16"/>
 
 <TextView
   android:id="@+id/tv_resident_desc"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="浦东新区 14:33发布"
   android:textSize="@dimen/sp_12"
   android:textColor="@color/color_999999"
   android:layout_marginEnd="@dimen/dp_10"
   android:layout_alignParentEnd="true"
   android:layout_below="@+id/tv_resident_aqi"
   android:layout_marginTop="@dimen/dp_11"/>
 
</RelativeLayout>

莫名奇妙啊简直

然后仔细想想可能的原因:难不成是因为分辨率适配的问题?

就是没有匹配到合适的分辨率的尺寸。那就试一下,把布局中所有引用@dimen的地方直接改为使用尺寸。

运行,成功!!!

问题:发现有个布局上的问题

自定义通知栏设置了背景,宽度是match_parent,但是发现在某些手机上,如小米6(截图所示),可以看到宽度竟然没有充满全屏。

但是在华为, vivo等手机上正常。

那就把这个背景去掉,自适应好了。

总结

到此这篇关于android实现常驻通知栏遇到的问题及解决办法的文章就介绍到这了,更多相关android 常驻通知栏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Android自定义Toolbar使用方法详解

    Android自定义Toolbar使用方法详解

    这篇文章主要为大家详细介绍了Android自定义Toolbar使用方法 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • Android利用Sensor实现传感器功能

    Android利用Sensor实现传感器功能

    这篇文章主要为大家详细介绍了Android利用Sensor实现传感器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • 浅谈Android App开发中Fragment的创建与生命周期

    浅谈Android App开发中Fragment的创建与生命周期

    这篇文章主要介绍了Android App开发中Fragment的创建与生命周期,文中详细地介绍了Fragment的概念以及一些常用的生命周期控制方法,需要的朋友可以参考下
    2016-02-02
  • 基于Android实现颜色渐变动画效果

    基于Android实现颜色渐变动画效果

    本文主要给大家介绍了Android实现颜色渐变动画效果,实现这样的一个动画渐变的效果很简单,只需要两步,第一步用GradientDrawable实现两个颜色之间的渐变效果,第二步用属性动画实现颜色变化的过程,需要的朋友可以参考下
    2024-01-01
  • Android 侧滑按钮的实现代码

    Android 侧滑按钮的实现代码

    这篇文章主要介绍了Android 侧滑按钮的实现,本文结合示例代码图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • Android给TextView添加点击事件的实现方法

    Android给TextView添加点击事件的实现方法

    下面小编就为大家带来一篇Android给TextView添加点击事件的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • 基于Android实现3D翻页效果

    基于Android实现3D翻页效果

    这篇文章主要为大家详细介绍了基于Android实现3D翻页效果的具体代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Android中的Coroutine协程原理解析

    Android中的Coroutine协程原理解析

    这篇文章主要介绍了Android中的Coroutine协程原理解析,本文将会围绕挂起与恢复彻底剖析协程的实现原理,包括Kotlin函数基础知识复习,关于函数的个人经验总结,需要的朋友可以参考下
    2022-03-03
  • Android基于OpenGL在GLSurfaceView上绘制三角形及使用投影和相机视图方法示例

    Android基于OpenGL在GLSurfaceView上绘制三角形及使用投影和相机视图方法示例

    这篇文章主要介绍了Android基于OpenGL在GLSurfaceView上绘制三角形及使用投影和相机视图方法,结合实例形式分析了Android基于OpenGL的图形绘制技巧,需要的朋友可以参考下
    2016-10-10
  • Android判断网络类型的方法(2g,3g还是wifi)

    Android判断网络类型的方法(2g,3g还是wifi)

    这篇文章主要介绍了Android判断网络类型的方法,可实现判断2g,3g还是wifi的功能,结合实例形式分析了Android针对网络类型的相关判定技巧,需要的朋友可以参考下
    2016-02-02

最新评论