久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

解決Android自定義view獲取attr中自定義顏色的問題

瀏覽:3日期:2022-09-21 14:41:57

好久不見,最近有點忙,好久沒有發文章了,前天有個小學弟在寫項目的過程中遇到了這個問題,由此寫一篇文章。

項目場景:

Android自定義View在布局中設置顏色

問題描述: 以下為學弟的代碼

//attrs文件 <attr name='leftcolor' format='color'/> <attr name='rightcolor' format='color'/>//Java文件代碼,獲取顏色 int leftcolor=attrs.getAttributeIntValue('http://schemas.android.com/apk/res-auto','leftcolor',Color.BLACK); int rightcolor=attrs.getAttributeIntValue('http://schemas.android.com/apk/res-auto','rightcolor',Color.WHITE);//布局中 app:leftcolor='@color/colorPrimary' app:rightcolor='#ff0000'

問題:rightcolor能夠獲取到顏色,而left不能獲取到。(寫#ff0000都能獲取到)

分析:

在寫了個Demo后,確實有類似問題,自己試了幾種辦法沒有解決,于是看了以下Android源碼控件,得出解決方案如下

解決方案:

//attrs文件 <attr name='leftcolor' format='reference|color'/> <attr name='rightcolor' format='reference|color'/>//java文件 ---TaiJiView為自定義view名稱 //獲取自定義屬性。 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TaiJiView); //獲取顏色 int leftcolor = ta.getColor(R.styleable.TaiJiView_leftcolor, Color.BLACK); int rightcolor=ta.getColor(R.styleable.TaiJiView_rightcolor, Color.WHITE); //回收 ta.recycle();//布局中 app:leftcolor='@color/colorPrimary' app:rightcolor='#ff0000'

如果搜到這篇文章,希望能夠解決您的問題。

到此這篇關于Android自定義view獲取attr中自定義顏色的問題的文章就介紹到這了,更多相關Android自定義view內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 新安县| 黄梅县| 东明县| 漳浦县| 岳阳市| 沧源| 正镶白旗| 大埔区| 嘉祥县| 徐水县| 恭城| 绿春县| 镇康县| 五原县| 泽普县| 泰宁县| 上高县| 沂南县| 滨海县| 阿城市| 宣恩县| 息烽县| 昂仁县| 建瓯市| 海兴县| 罗江县| 佳木斯市| 吐鲁番市| 宁南县| 常山县| 定襄县| 博罗县| 新乡市| 深泽县| 安阳市| 河西区| 文化| 任丘市| 卓尼县| 萍乡市| 涞水县|