AndroidにおけるAPIのDrawable資源の詳細及び簡単な例

6598 ワード

AndroidにおけるAPIのDrawable資源
1、一番よく使うStteListDrawable
 StteListDrawableというと、Android猿の多くはあまり馴染みがないと感じるかもしれませんが、selectorセレクタと言えば、きっとはっと悟ります。いいです。この二つは同じです。
その用途の広さは、アプリごとに必ず使われます。以下はデモを書いて、使い方を簡単に説明します。
例えば、ログイン画面では、入力ボックスがフォーカスを取得する際に背景を変更する必要があります。登録ボタンが入力ボックスに内容がある場合は、背景色を変更します。この場合は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ショッピングselectedとicon_ショッピング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を設定すると、次のような問題が発生します。

対応する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は2つの部分から構成されています。一部は画像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); 
    } 
     
  } 
   
   
} 

 読んでくれてありがとうございます。みなさんのご協力をお願いします。ありがとうございます。