사용자 정의 버튼 상태를 추가하는 방법
예를 들어 기본 버튼은 상태와 배경 이미지 사이에 다음과 같은 종속성이 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/btn_default_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/btn_default_normal_disable" />
<item android:state_pressed="true"
android:drawable="@drawable/btn_default_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/btn_default_selected" />
<item android:state_enabled="true"
android:drawable="@drawable/btn_default_normal" />
<item android:state_focused="true"
android:drawable="@drawable/btn_default_normal_disable_focused" />
<item
android:drawable="@drawable/btn_default_normal_disable" />
</selector>
내 자신의 사용자 지정 상태 (예 : smth android:state_custom
)를 어떻게 정의 할 수 있습니까? 그러면이를 사용하여 버튼의 시각적 모양을 동적으로 변경할 수 있습니까?
@ (Ted Hopp)로 표시된 솔루션은 작동하지만 약간의 수정이 필요합니다. 선택기에서 항목 상태에 "app :"접두사가 필요합니다. 그렇지 않으면 팽창기가 네임 스페이스를 올바르게 인식하지 못하고 자동으로 실패합니다. 적어도 이것은 나에게 일어나는 일입니다.
여기에 전체 솔루션을 좀 더 자세히 설명해 드리겠습니다.
먼저 "res / values / attrs.xml"파일을 작성하십시오.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="food">
<attr name="state_fried" format="boolean" />
<attr name="state_baked" format="boolean" />
</declare-styleable>
</resources>
그런 다음 사용자 정의 클래스를 정의하십시오. 예를 들어, "Button"클래스에서 파생 된 "FoodButton"클래스 일 수 있습니다. 생성자를 구현해야합니다. 이 것을 구현하십시오. 인플레이터가 사용하는 것으로 보입니다.
public FoodButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
파생 클래스 위에 :
private static final int[] STATE_FRIED = {R.attr.state_fried};
private static final int[] STATE_BAKED = {R.attr.state_baked};
또한 상태 변수 :
private boolean mIsFried = false;
private boolean mIsBaked = false;
그리고 두 세터 :
public void setFried(boolean isFried) {mIsFried = isFried;}
public void setBaked(boolean isBaked) {mIsBaked = isBaked;}
그런 다음 "onCreateDrawableState"함수를 재정의하십시오.
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
if (mIsFried) {
mergeDrawableStates(drawableState, STATE_FRIED);
}
if (mIsBaked) {
mergeDrawableStates(drawableState, STATE_BAKED);
}
return drawableState;
}
마지막으로이 퍼즐의 가장 섬세한 부분입니다. 위젯의 배경으로 사용할 StateListDrawable을 정의하는 선택기 파일 "res / drawable / food_button.xml"입니다.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.mydomain.mypackage">
<item
app:state_baked="true"
app:state_fried="false"
android:drawable="@drawable/item_baked" />
<item
app:state_baked="false"
app:state_fried="true"
android:drawable="@drawable/item_fried" />
<item
app:state_baked="true"
app:state_fried="true"
android:drawable="@drawable/item_overcooked" />
<item
app:state_baked="false"
app:state_fried="false"
android:drawable="@drawable/item_raw" />
</selector>
Notice the "app:" prefix, whereas with standard android states you would have used prefix "android:". The XML namespace is crucial for a correct interpretation by the inflater and depends on the type of project in which you are adding attributes. If it is an application, replace com.mydomain.mypackage with the actual package name of your application (application name excluded). If it is a library you must use "http://schemas.android.com/apk/res-auto" (and be using Tools R17 or later) or you will get runtime errors.
A couple of notes:
It seems you don't need to call the "refreshDrawableState" function, at least the solution works well as is, in my case
In order to use your custom class in a layout xml file, you will have to specify the fully qualified name (e.g. com.mydomain.mypackage.FoodButton)
You can as weel mix-up standard states (e.g. android:pressed, android:enabled, android:selected) with custom states, in order to represent more complicated state combinations
This thread shows how to add custom states to buttons and the like. (If you can't see the new Google groups in your browser, there's a copy of the thread here.)
Please do not forget to call refreshDrawableState
within UI thread:
mHandler.post(new Runnable() {
@Override
public void run() {
refreshDrawableState();
}
});
It took lot of my time to figure out why my button is not changing its state even though everything looks right.
참고URL : https://stackoverflow.com/questions/4336060/how-to-add-a-custom-button-state
'Programing' 카테고리의 다른 글
콘다 환경 제거 (0) | 2020.07.11 |
---|---|
OSError : [Errno 2] Django에서 Python 하위 프로세스를 사용하는 동안 해당 파일이나 디렉토리가 없습니다 (0) | 2020.07.11 |
Cygwin에 적합합니까? (0) | 2020.07.11 |
유닉스 / 리눅스에서 프로세스의 경로를 얻는 방법 (0) | 2020.07.11 |
C #에서 클래스는 다른 클래스와 인터페이스에서 상속 할 수 있습니까? (0) | 2020.07.11 |