国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 中API之Drawable資源詳解及簡單實例

2019-10-23 18:29:51
字體:
來源:轉載
供稿:網友

Android 中API之Drawable資源

1、最常用的StateListDrawable

 說StateListDrawable,很多Android猿可能感到不太熟悉,不過如果說selector選擇器,肯定都會恍然大悟,不錯,這兩個東西就是同一個~~

它的用途之廣,每個app必用,下面就寫一個demo,來簡要說一下用法。

比如一個登陸界面,它的輸入框在獲取焦點時需要更改背景,登陸按鈕在輸入框中有內容時,則更改背景顏色,這時候用selector選擇器,那就方便多了,效果如下:     

Android,Drawable,Drawable使用方法Android,Drawable,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,Drawable,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);     }        }       } 

 感謝 閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 固阳县| 广饶县| 五原县| 凤城市| 蒙自县| 高州市| 万源市| 大理市| 蒙城县| 北京市| 崇左市| 长武县| 天津市| 清河县| 大石桥市| 大埔区| 湘阴县| 利辛县| 汽车| 泸州市| 清水县| 富锦市| 鲁甸县| 河南省| 定西市| 宁海县| 洪雅县| 青海省| 旺苍县| 浏阳市| 黎平县| 甘孜县| 岑溪市| 沾益县| 北海市| 新营市| 简阳市| 大厂| 宁强县| 简阳市| 凤山县|