Android 에서 API 의 Drawable 자원 상세 설명 및 간단 한 인 스 턴 스

7026 단어 AndroidDrawable
Android 에서 API 의 Drawable 자원
1.가장 많이 사용 되 는 StateListDrawable
 StateList Drawable 이 라 고 하면 많은 안 드 로 이 드 원숭이 들 이 익숙 하지 않 을 수도 있 지만 selector 선택 기 라 고 하면 모두 문득 깨 닫 게 될 것 입 니 다.좋 습 니 다.이 두 가지 가 똑 같 습 니 다~~
용도 가 넓 습 니 다.모든 app 은 반드시 사용 해 야 합 니 다.다음은 demo 를 써 서 용법 을 간략하게 설명 하 겠 습 니 다.
예 를 들 어 로그 인 인터페이스 의 입력 상 자 는 초점 을 가 져 올 때 배경 을 변경 해 야 합 니 다.로그 인 단 추 는 입력 상자 에 내용 이 있 을 때 배경 색 을 변경 합 니 다.이때 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 와 iconshopping_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 는 두 부분 으로 구성 되 어 있 으 며,일 부 는 이미지 이미지 뷰 이 고,다른 일 부 는 문자 내용 이기 때문에 이 문 제 를 해결 하려 면 위의 설정 방식 에 따라 하면 된다.
자바 코드 는 간단 합 니 다.다음 과 같 습 니 다.

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

 읽 어 주 셔 서 감사합니다. 여러분 에 게 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기