android remoteviews 设置背景,Android View | RemoteViews使用及原理

1. 应用场景

通知栏

桌面小控件

2. RemoteViews 原理

2.1 RemoteViews 不是一个 View

public class RemoteViews implements Parcelable, Filter {

}

从类的定义能够看出 RemoteViews 实际上并不是一个 View,它只是一个实现了 Parcelable 接口的类,为什么要实现 Parcelable 接口呢,这是因为我们在跨进程更新布局的时候需要传递 RemoteViews,而跨进程通信中传递的数据除了基本数据类型外,对于自定义的数据类型一定要实现 Parcelable 接口才能在进程间实现数据的传递。

2.2 RemoteViews 不支持自定义的布局和控件

RemoteViews 支持的布局和控件是有限的,支持的布局有:

FrameLayout

GridLayout

GridView

LinearLayout

ListView

RelativeLayout

StackView

ViewFlipper

AdapterViewFlipper

支持的控件有:

Button

ImageButton

TextView

TextClock

ImageView

ProgressBar

Chronometer

AnalogClock

注意

在 RemoteViews 的 XML 布局中使用了 ImageView,按道理来说 RemoteViews 是支持的。实际应用中,在 AppCompatActivity 布局中使用的 ImageView 等会在加载布局时被转换成对应的兼容类即 AppCompatImageView,兼容类都是 ImageView 的子类,有如下错误提示:

Caused by: android.widget.RemoteViews ActionException:android.widget.RemoteViews$ActionException:

view:android.support.v7.widget.AppCompatImageView can't use method with RemoteViews: setImageResource(int)

错误提示表明在 RemoteViews 中 AppCompatImageView 不能使用 setImageResource() 方法,这种情况下我们将 AppCompatActivity 改为 Activity,不使用 AppCompatActivity 就可以避免将布局中的 ImageView 转换为 AppCompatImageView,或者在 build.gradle 中设置 appcompat 为 compile ‘com.android.support:appcompat-v7:23.0.1’ 或更低。

原因:IamgeView 的 setImageResource() 方法有注解 @android.view.RemotableViewMethod(asyncImpl="setImageResourceAsync"

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值