欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 中API之Drawable资源详解及简单实例

程序员文章站 2023-08-15 10:20:49
android 中api之drawable资源 1、最常用的statelistdrawable  说statelistdrawable,很多android...

android 中api之drawable资源

1、最常用的statelistdrawable

 说statelistdrawable,很多android猿可能感到不太熟悉,不过如果说selector选择器,肯定都会恍然大悟,不错,这两个东西就是同一个~~

它的用途之广,每个app必用,下面就写一个demo,来简要说一下用法。

比如一个登陆界面,它的输入框在获取焦点时需要更改背景,登陆按钮在输入框中有内容时,则更改背景颜色,这时候用selector选择器,那就方便多了,效果如下:     

Android 中API之Drawable资源详解及简单实例Android 中API之Drawable资源详解及简单实例

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的布局文件中的设置,如下:

<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的设置单独列出来,是因为这里有个坑。想要自己定制checkbox的图片,只需要给android:button赋值即可,但为赋值之后,没办法设置padding值,而一般来说,checkbox给的图片可能会很小,需要设置一些padding。如果将selector选择器设置给button属性,再设置padding,就会造成下面的问题,

Android 中API之Drawable资源详解及简单实例

对应的xml设置如下:

<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,另一部分是文字内容,想要解决这个问题,按照上面的设置方式即可。

java代码很简单,如下:

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); 
    } 
     
  } 
   
   
} 

 感谢 阅读,希望能帮助到大家,谢谢大家对本站的支持!