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);
}
}
}
읽 어 주 셔 서 감사합니다. 여러분 에 게 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.