안드로이드 버튼 선택기
이 버튼 선택기는 정상일 때 빨간색으로, 눌렀을 때 회색으로 표시되는 버튼 선택기입니다.
PRESSED 할 때 텍스트 크기와 색상이 변경되도록 코드를 직접 추가로 수정하는 방법을 묻고 싶습니다. 많은 감사합니다!
<item android:state_pressed="true" >
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp" android:color="@color/black" />
<solid android:color="@color/grey"/>
<padding android:left="5dp" android:top="2dp"
android:right="5dp" android:bottom="2dp" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp" android:color="@color/black" />
<solid android:color="#FF6699"/>
<padding android:left="5dp" android:top="2dp"
android:right="5dp" android:bottom="2dp" />
<corners android:radius="5dp" />
</shape>
</item>
레이아웃 파일에서 설정 selector
하면 button
됩니다.
<Button
android:id="@+id/button1"
android:background="@drawable/selector_xml_name"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
그리고 완료.
편집하다
다음은 디렉토리의 button_effect.xml
파일입니다.drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/numpad_button_bg_normal"></item>
</selector>
이것에서, 당신은 3 개의 드로어 블이 있음을 알 수 있습니다 . 위에 쓴 것처럼 이 button_effect
스타일을 에 배치해야합니다 button
. 당신은 교체해야 selector_xml_name
와 함께 button_effect
.
상태 목록 드로어 블로 텍스트 크기를 변경할 수 없습니다 . 텍스트 색상과 텍스트 크기를 변경하려면 다음과 같이하십시오 :
글자 색
텍스트 색상을 변경하기 위해 색상 상태 목록 리소스를 만들 수 있습니다 . res/color/
디렉토리 에있는 별도의 리소스가됩니다 . 레이아웃 xml에서이를 android:textColor
속성 값으로 설정해야 합니다. 색상 선택기에는 다음과 같은 내용이 포함됩니다.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/text_pressed" />
<item android:color="@color/text_normal" />
</selector>
문자 크기
단순히 리소스를 사용하여 텍스트 크기를 변경할 수 없습니다. "dimen selector"는 없습니다. 코드에서해야합니다. 그리고 직접적인 해결책은 없습니다.
아마도 가장 쉬운 솔루션은 View.onTouchListener()
up 및 down 이벤트를 적절히 활용 하고 처리하는 것일 수 있습니다 . 다음과 같이 사용하십시오 :
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// change text size to the "pressed value"
return true;
case MotionEvent.ACTION_UP:
// change text size to the "normal value"
return true;
default:
return false;
}
}
});
A different solution might be to extend the view and override the setPressed(Boolean)
method. The method is internally called when the change of the pressed state happens. Then change the size of the text accordingly in the method call (don't forget to call the super).
Create custom_selector.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/unselected" android:state_pressed="true" />
<item android:drawable="@drawable/selected" />
</selector>
Create selected.xml shape in drawable folder
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp">
<solid android:color="@color/selected"/>
<padding />
<stroke android:color="#000" android:width="1dp"/>
<corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
</shape>
Create unselected.xml shape in drawable folder
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp">
<solid android:color="@color/unselected"/>
<padding />
<stroke android:color="#000" android:width="1dp"/>
<corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
</shape>
Add following colors for selected/unselected state in color.xml of values folder
<color name="selected">#a8cf45</color>
<color name="unselected">#ff8cae3b</color>
you can check complete solution from here
Best way to implement the selector is by using the xml instead of using programatic way as its more easy to implemnt with xml.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_bg_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/button_bg_normal"></item>
</selector>
For more information i implemented using this link http://www.blazin.in/2016/03/how-to-use-selectors-for-botton.html
In Layout .xml file
<Button
android:id="@+id/button1"
android:background="@drawable/btn_selector"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="press" />
btn_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="@drawable/btn_bg_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/btn_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/btn_bg_normal"></item>
You can use this code:
<Button
android:id="@+id/img_sublist_carat"
android:layout_width="70dp"
android:layout_height="68dp"
android:layout_centerVertical="true"
android:layout_marginLeft="625dp"
android:contentDescription=""
android:background="@drawable/img_sublist_carat_selector"
android:visibility="visible" />
(Selector File) img_sublist_carat_selector.xml:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/img_sublist_carat_highlight" />
<item android:state_pressed="true"
android:drawable="@drawable/img_sublist_carat_highlight" />
<item android:drawable="@drawable/img_sublist_carat_normal" />
</selector>
참고URL : https://stackoverflow.com/questions/14023886/android-button-selector
'IT박스' 카테고리의 다른 글
트위터의 부트 스트랩 2.1.0에서 새로운 접사 플러그인을 사용하는 방법은 무엇입니까? (0) | 2020.07.30 |
---|---|
iframe에서 가로 스크롤 막대를 숨기시겠습니까? (0) | 2020.07.30 |
자바 스크립트로 텍스트 선택 지우기 (0) | 2020.07.30 |
리터럴 키로 PHP 접두사 연관 배열? (0) | 2020.07.30 |
Java의 다중 스레드 환경에서 정적 메소드 동작 (0) | 2020.07.30 |