欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決Android自定義view獲取attr中自定義顏色的問(wèn)題

 更新時(shí)間:2020年12月08日 14:40:36   作者:計(jì)蒙不吃魚  
這篇文章主要介紹了Android自定義view獲取attr中自定義顏色的問(wèn)題解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

好久不見(jiàn),最近有點(diǎn)忙,好久沒(méi)有發(fā)文章了,前天有個(gè)小學(xué)弟在寫項(xiàng)目的過(guò)程中遇到了這個(gè)問(wèn)題,由此寫一篇文章。

項(xiàng)目場(chǎng)景:

Android自定義View在布局中設(shè)置顏色

問(wèn)題描述: 以下為學(xué)弟的代碼

//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"

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

分析:

在寫了個(gè)Demo后,確實(shí)有類似問(wèn)題,自己試了幾種辦法沒(méi)有解決,于是看了以下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"

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

到此這篇關(guān)于Android自定義view獲取attr中自定義顏色的問(wèn)題的文章就介紹到這了,更多相關(guān)Android自定義view內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論