- 답변 # 1
- 답변 # 2
이것이 ImageButton이고 각 프레스/압축되지 않은 상태에 대해 여러 드로어 블을 사용하지 않으려면 이미지의 컬러 필터 속성을 사용할 수 있습니다. 이 솔루션은 Omar가 사용하는 솔루션과 유사합니다.
터치시 컬러 필터를 수정하는 OnTouchListener를 만듭니다.
public class ButtonHighlighterOnTouchListener implements OnTouchListener { final ImageButton imageButton; public ButtonHighlighterOnTouchListener(final ImageButton imageButton) { super(); this.imageButton = imageButton; } public boolean onTouch(final View view, final MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { //grey color filter, you can change the color as you like imageButton.setColorFilter(Color.argb(155, 185, 185, 185)); } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) { imageButton.setColorFilter(Color.argb(0, 185, 185, 185)); } return false; } }
이 리스너를 버튼에 할당하십시오 :
myButton = (ImageButton) findViewById(R.id.myButton); myButton.setOnTouchListener(new ButtonHighlighterOnTouchListener(myButton));
업데이트
복합 드로어 블을 통해 ImageView, ImageButton 또는 TextView에 형광펜을 적용하는 클래스가 개선되었습니다.
public class ButtonHighlighterOnTouchListener implements OnTouchListener { private static final int TRANSPARENT_GREY = Color.argb(0, 185, 185, 185); private static final int FILTERED_GREY = Color.argb(155, 185, 185, 185); ImageView imageView; TextView textView; public ButtonHighlighterOnTouchListener(final ImageView imageView) { super(); this.imageView = imageView; } public ButtonHighlighterOnTouchListener(final TextView textView) { super(); this.textView = textView; } public boolean onTouch(final View view, final MotionEvent motionEvent) { if (imageView != null) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { imageView.setColorFilter(FILTERED_GREY); } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) { imageView.setColorFilter(TRANSPARENT_GREY); // or null } } else { for (final Drawable compoundDrawable : textView.getCompoundDrawables()) { if (compoundDrawable != null) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { // we use PorterDuff.Mode. SRC_ATOP as our filter color is already transparent // we should have use PorterDuff.Mode.LIGHTEN with a non transparent color compoundDrawable.setColorFilter(FILTERED_GREY, PorterDuff.Mode.SRC_ATOP); } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) { compoundDrawable.setColorFilter(TRANSPARENT_GREY, PorterDuff.Mode.SRC_ATOP); // or null } } } } return false; } }
- 답변 # 3
여기를 참조하십시오
그리고 여기도 Romain Guy의 답변 :
와이즈 비즈In res/drawable, create a file called for instance mybutton_background.xml and put something like this inside:
그런 다음이 드로어 블을 버튼의 배경으로 설정하십시오. 와이즈 비즈
- 답변 # 4
사용중인 경우
<?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="false" android:drawable="@drawable/button_background_focus" /> <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/button_background_pressed" /> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/button_background_pressed" /> <item android:drawable="@drawable/button_background_normal" /> </selector>
그러면 새로운 XML을 새로 선언하는 것이 좋습니다. 이미지를 사용하면 각 이벤트 상태에 대한 이미지를 지정할 수 있습니다. Octavian이 말한 것처럼
배경으로이 XML을 설정할 수 있습니다xml이 'res/drawable/abc.xml'인 경우 배경을 다음으로 설정
android:background="@drawable/mybutton_background"
ImageButton을 사용할 수도 있습니다
<Button ------------------ android:background="@drawable/youimage" >
ImageButton의 장점은 하이라이트를 위해 다른 이미지가 필요하지 않다는 것입니다.
- 답변 # 5
이미지 사본을 여러 개 만들 필요없이 가난한 사람의 하이라이트 효과를 얻으려면이 방법을 사용하십시오. 알파 값 0.8을 원하는 값으로 설정하십시오 :
android:background="@drawable/abc"
<ImageButton ---------------------- android:src="@drawable/youimage" />
관련 자료
- javascript - 버튼을 두 번 이상 클릭 할 수있게 만드는 방법
- ntp - RTC가 정확한지 확인하는 방법
- java - 원형보기 pager2를 만드는 방법은 무엇입니까?
- 데비안 용 apt 또는 deb 패키지는 어떻게 만드나요?
- python - long if else 및 문자열 일반을 어떻게 만들 수 있습니까?
- c++ - std - : initializer_list 로 생성자를 만드는 방법
- c - fscanf 함수가 한 줄을 건너 뛰도록하는 방법은 무엇입니까?
- c# - net Core 31 콘솔 앱에서 콘솔 로거를 작동시키는 방법
- 이 파이썬 패턴을 만드는 방법?
- python - 스크롤 가능한 스크롤바는 어떻게 만듭니 까?
- html - SVG를 의사 요소에 맞추는 방법
- linux - C 언어로 일기를 어떻게 만드나요?
- 파이썬에 버튼을 추가하는 방법
- typescript의 객체 키에서 객체 유형을 만드는 방법은 무엇입니까?
- javascript - 배열을 변경 불가능하게 만드는 방법
- latex-tikz의 노드에서 상자로 다중 에지/트리 에지를 만들려면 어떻게합니까?
- linux - C에서 프롬프트를 만드는 방법
- android - OptionsMenu 열기 버튼 설정 방법
- How to make foreach work with this PHP script - 이 php 스크립트로 foreach를 작동시키는 방법 - get api
- rpg 언어 - 루프를 만드는 방법?
- OpenCv의 폴더에서 여러 이미지 읽기 (python)
- 파이썬 셀레늄 모든 "href"속성 가져 오기
- html - 자바 스크립트 - 클릭 후 변경 버튼 텍스트 변경
- javascript - 현재 URL에서 특정 div 만 새로 고침/새로 고침
- JSP에 대한 클래스를 컴파일 할 수 없습니다
- JavaScript 변수를 HTML div에 '출력'하는 방법
- git commit - 자식 - 로컬 커밋 된 파일에 대한 변경을 취소하는 방법
- jquery - JavaScript로 현재 세션 값을 얻으시겠습니까?
- javascript - swiperjs에서 정지, 재생 버튼 추가
- python - 화면에서 찾은 요소를 찾을 수없는 경우 셀레늄
올바른 리소스를 얻으려면 StateList Drawable의 문서가 있습니다. 당신은 당신의
res/drawable
에서 그것을 만들고 정의합니다. 하위 디렉토리를 지정하고 버튼 배경으로 설정하십시오.