Android 中API之Drawable資源詳解及簡(jiǎn)單實(shí)例
Android 中API之Drawable資源
1、最常用的StateListDrawable
說(shuō)StateListDrawable,很多Android猿可能感到不太熟悉,不過(guò)如果說(shuō)selector選擇器,肯定都會(huì)恍然大悟,不錯(cuò),這兩個(gè)東西就是同一個(gè)~~
它的用途之廣,每個(gè)app必用,下面就寫(xiě)一個(gè)demo,來(lái)簡(jiǎn)要說(shuō)一下用法。
比如一個(gè)登陸界面,它的輸入框在獲取焦點(diǎn)時(shí)需要更改背景,登陸按鈕在輸入框中有內(nèi)容時(shí),則更改背景顏色,這時(shí)候用selector選擇器,那就方便多了,效果如下:


EditText的背景xml如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_focused="true" android:drawable="@drawable/et_focus"/> <item android:state_focused="false" android:drawable="@drawable/et_unfocus"/> </selector>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="2dp"/> <stroke android:width="1px" android:color="#f85355" /> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="2dp"/> <stroke android:width="1px" android:color="#c9caca" /> </shape>
提交TextView的背景xml如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_enabled="true" android:drawable="@drawable/btn_enable"/> <item android:state_enabled="false" android:drawable="@drawable/btn_unenable"/> </selector>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="5dp"/> <solid android:color="#f85355"/> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="5dp"/> <solid android:color="#c9caca"/> </shape>
CheckBox的xml如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="true" android:drawable="@drawable/icon_shopping_selected"/> <item android:state_checked="false" android:drawable="@drawable/icon_shopping_unselected"/> </selector>
icon_shopping_selected和icon_shopping_unselected是2張圖片,下面是CheckBox在activity的布局文件中的設(shè)置,如下:
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:checked="true"
android:button="@null"
android:drawableLeft="@drawable/cb_agree"
android:padding="20dp" />
之所以把CheckBox的設(shè)置單獨(dú)列出來(lái),是因?yàn)檫@里有個(gè)坑。想要自己定制CheckBox的圖片,只需要給android:button賦值即可,但為賦值之后,沒(méi)辦法設(shè)置padding值,而一般來(lái)說(shuō),CheckBox給的圖片可能會(huì)很小,需要設(shè)置一些padding。如果將selector選擇器設(shè)置給button屬性,再設(shè)置padding,就會(huì)造成下面的問(wèn)題,

對(duì)應(yīng)的xml設(shè)置如下:
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:checked="true"
android:button="@drawable/cb_agree"
android:padding="20dp"
android:background="#00ff00"/>
造成這種情況的原因是,CheckBox是由兩部分組成的,一部分是圖片ImageView,另一部分是文字內(nèi)容,想要解決這個(gè)問(wèn)題,按照上面的設(shè)置方式即可。
Java代碼很簡(jiǎn)單,如下:
public class StateListDrawableActivity extends Activity{
private TextView mSubmit;
private EditText mPhoneView;
private EditText mPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_state_list_drawable);
mPhoneView = (EditText) findViewById(R.id.et_phone);
mPassword = (EditText) findViewById(R.id.et_password);
BaseTextWatcher watcher = new BaseTextWatcher();
watcher.addEditText(mPhoneView,mPassword);
mSubmit = (TextView) findViewById(R.id.tv_state_list_drawable);
}
class BaseTextWatcher implements TextWatcher{
private ArrayList<EditText> list = new ArrayList<EditText>();
public void addEditText(EditText...ets){
for(int i=0;i<ets.length;i++){
ets[i].addTextChangedListener(this);
list.add(ets[i]);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
for(EditText et:list){
String text = et.getText().toString().trim();
if(TextUtils.isEmpty(text)){
return;
}
}
mSubmit.setEnabled(true);
}
}
}
感謝 閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android setButtonDrawable()的兼容問(wèn)題解決辦法
- Android DrawableTextView圖片文字居中顯示實(shí)例
- Android中EditText的drawableRight屬性設(shè)置點(diǎn)擊事件
- 關(guān)于Android中drawable必知的一些規(guī)則
- Android 讓自定義TextView的drawableLeft與文本一起居中
- 如何玩轉(zhuǎn)Android矢量圖VectorDrawable
- Android App開(kāi)發(fā)中將View或Drawable轉(zhuǎn)為Bitmap的方法
- Android編程中TextView寬度過(guò)大導(dǎo)致Drawable無(wú)法居中問(wèn)題解決方法
- android中圖形圖像處理之drawable用法分析
相關(guān)文章
Android 7.0開(kāi)發(fā)獲取存儲(chǔ)設(shè)備信息的方法
這篇文章主要介紹了Android 7.0開(kāi)發(fā)獲取存儲(chǔ)設(shè)備信息的方法,結(jié)合實(shí)例形式分析了Android7.0針對(duì)存儲(chǔ)設(shè)備信息的獲取、判斷操作方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-11-11
Flutter下Android Studio配置gradle的方法
這篇文章主要介紹了Flutter下Android Studio配置gradle的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android自定義View實(shí)現(xiàn)繪制虛線的方法詳解
這篇文章主要給大家介紹了Android自定義View實(shí)現(xiàn)繪制虛線的方法,在繪制過(guò)程中走了一些彎路才實(shí)現(xiàn)了虛線的效果,所以想著總結(jié)分享出來(lái),方便有需要的朋友和自己在需要的時(shí)候參考學(xué)習(xí),下面來(lái)一起看看吧。2017-04-04
Android如何使用正則表達(dá)式只保留字母數(shù)字
在做項(xiàng)目的過(guò)程中,使用正則表達(dá)式來(lái)匹配一段文本中的特定種類(lèi)字符,是比較常用的一種方式,下面這篇文章主要給大家介紹了關(guān)于Android如何使用正則表達(dá)式只保留字母數(shù)字的相關(guān)資料,需要的朋友可以參考下2022-05-05
Android多線程及異步處理問(wèn)題詳細(xì)探討
究其為啥需要多線程的本質(zhì)就是異步處理,直觀一點(diǎn)說(shuō)就是不要讓用戶(hù)感覺(jué)到“很卡”為了提高用戶(hù)體驗(yàn)?zāi)鞘潜仨氁褂玫?/div> 2013-06-06
Android WebView攔截iframe標(biāo)簽內(nèi)部跳轉(zhuǎn)教程
這篇文章主要介紹了Android WebView攔截iframe標(biāo)簽內(nèi)部跳轉(zhuǎn)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android應(yīng)用中實(shí)現(xiàn)選擇本地文件與目錄的實(shí)例分享
這篇文章主要介紹了Android應(yīng)用中實(shí)現(xiàn)選擇本地文件與目錄的實(shí)例分享,相當(dāng)于從app內(nèi)呼出的簡(jiǎn)易的資源管理器來(lái)使用本地資源,需要的朋友可以參考下2016-02-02
Android學(xué)習(xí)筆記--通過(guò)Application傳遞數(shù)據(jù)代碼示例
使用Application傳遞數(shù)據(jù)步驟如下:創(chuàng)建新class,取名MyApp,繼承android.app.Application父類(lèi),并在MyApp中定義需要保存的屬性2013-06-06最新評(píng)論

