활동에 대한 단추를 선언하고 android:onClick
속성을 설정 하면 활동이 누르면 단추의 배경색이 자동으로 변경됩니다.
이제 조각에 버튼이 있습니다. android:onClick
여기서는 사용할 수 없기 때문에 onClickListener
대신 a를 사용합니다. 버튼은 작동하지만 더 이상 스타일을 변경하지 않습니다.
수동으로해야합니까? 어떻게?
XML 파일, drawable
선택기 가 필요합니다 . 이 파일의 이름은 다음과 res/drawable/bg_button.xml
같습니다.
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@drawable/bg_button_pressed"/>
<item android:state_pressed="true" android:drawable="@drawable/bg_button_pressed" />
<item android:drawable="@drawable/bg_button_normal" /> <!-- normal state -->
</selector>
대상 res/drawable/bg_button_pressed.xml
:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="color when pressed" />
</shape>
새 XML 파일을 res/drawable/bg_button_normal.xml
만들고 이름을 지정한 다음 위의 코드를 복사합니다. 그런 다음 android:color
버튼이 정상 상태 일 때 일반 색상으로 교체 합니다.
그리고 마지막으로:
<Button
android:id="@+id/..."
android:text="@string/..."
android:background="@drawable/bg_button"/>
res/drawable
폴더 가없는 경우 새로 만듭니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다