java typedarray_java – TypedArray .getColor()始终在自定义视图中返回-1

我创建了一个自定义视图,它将TextView的子类称为TextViewEx.此视图通过xml为TextView提供的复合Drawable增加了更多的灵活性.我想要的部分功能是能够对复合drawable进行着色,但无论出于何种原因,返回的颜色始终为-1.这是代码:

attrs.xml:

TextViewEx.java:

public class TextViewEx

extends TextView {

public TextViewEx(Context context) {

super(context);

init(null, 0);

}

public TextViewEx(Context context, AttributeSet attrs) {

super(context, attrs);

init(attrs, 0);

}

public TextViewEx(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init(attrs, defStyle);

}

private void init(AttributeSet attrs, int defStyle) {

if (attrs==null) {

return;

}

TypedArray a = getContext().obtainStyledAttributes(

attrs, R.styleable.TextViewEx, defStyle, 0);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 TypedArray 实现类似于 Node.js Buffer 的 TypeScript 类型的示例代码: ```typescript type TypedArray = Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array; class Buffer { private bytes: Uint8Array; constructor(sizeOrArray: number | Uint8Array) { if (typeof sizeOrArray === 'number') { this.bytes = new Uint8Array(sizeOrArray); } else { this.bytes = sizeOrArray; } } static from(value: string, encoding: string = 'utf-8'): Buffer { const encoder = new TextEncoder(); const bytes = encoder.encode(value); return new Buffer(bytes); } toString(encoding: string = 'utf-8'): string { const decoder = new TextDecoder(encoding); return decoder.decode(this.bytes); } static isBuffer(obj: any): obj is Buffer { return obj instanceof Buffer; } slice(start?: number, end?: number): Buffer { return new Buffer(this.bytes.subarray(start, end)); } // 将 TypedArray 转换为 Buffer static fromTypedArray(typedArray: TypedArray): Buffer { return new Buffer(new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength)); } // 将 Buffer 转换为 TypedArray toTypedArray(): TypedArray { return new Uint8Array(this.bytes.buffer, this.bytes.byteOffset, this.bytes.byteLength) as TypedArray; } } ``` 这个 Buffer 类型实现了一些常用的方法,例如 from、toString、slice 等。同时,还提供了 fromTypedArray 和 toTypedArray 两个方法,用于将 TypedArray 转换为 Buffer,或将 Buffer 转换为 TypedArray。 需要注意的是,由于不同浏览器对 TypedArray 的支持程度不同,一些 API 的实现和行为可能会有所不同,需要根据具体情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值