3
사용자 정의보기를 만들었습니다.이보기는 눌렀거나 강조 표시되거나 비활성화되었을 때 배경 이미지를 변경해야합니다. 앱이 실행되지만 버튼이 배경을 변경하지 않습니다. 내 XML사용자 정의보기가 터치에 응답하지 않습니다.
다음public class CustomImageButton extends View {
public CustomImageButton(Context context) {
super(context);
setFocusable(true);
setClickable(true);
}
public CustomImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
setFocusable(true);
setClickable(true);
}
public CustomImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFocusable(true);
setClickable(true);
}
protected Drawable background = super.getBackground();
@Override
public void setBackgroundDrawable(Drawable d) {
// Replace the original background drawable (e.g. image) with a LayerDrawable that
// contains the original drawable slightly edited.
CustomImageButtonBackgroundDrawable layer = new CustomImageButtonBackgroundDrawable(d);
super.setBackgroundDrawable(layer);
}
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int drawableWidth = super.getBackground().getMinimumWidth();
int drawableHeight = super.getBackground().getMinimumHeight();
setMeasuredDimension(drawableWidth, drawableHeight);
}
protected class CustomImageButtonBackgroundDrawable extends LayerDrawable {
protected Drawable lowerlayer;
protected Drawable _highlightedDrawable;
protected int _disabledAlpha = 100;
protected Drawable _pressedDrawable;
public CustomImageButtonBackgroundDrawable(Drawable d) {
super(new Drawable[] { d });
}
@Override
protected boolean onStateChange(int[] states) {
boolean enabled = false;
boolean highlighted = false;
boolean pressed = false;
for (int state : states) {
if (state == android.R.attr.state_enabled)
enabled = true;
else if (state == android.R.attr.state_selected)
highlighted = true;
else if (state == android.R.attr.state_pressed)
pressed = true;
}
mutate();
if (enabled && highlighted) {
ColorFilter colourFilter = new LightingColorFilter(Color.YELLOW, 1);
ScaleDrawable resizedImage = new ScaleDrawable(background, 0, 1.25f, 1.25f);
lowerlayer = resizedImage.getDrawable();
lowerlayer.setColorFilter(colourFilter);
Drawable[] aD = new Drawable[2];
aD[0] = lowerlayer;
aD[1] = background;
LayerDrawable _highlightedDrawable = new LayerDrawable(aD);
setBackgroundDrawable(_highlightedDrawable); // buttons need transparent backgrounds
} else if (!enabled) {
setColorFilter(null);
setAlpha(_disabledAlpha);
} else if (enabled && pressed){
ScaleDrawable smaller = new ScaleDrawable(background, 0, 0.75f, 0.75f);
setBackgroundDrawable(smaller.getDrawable());
} else if(enabled){
setBackgroundDrawable(background);
setColorFilter(null);
}
invalidateSelf();
return super.onStateChange(states);
}
}
}
을 것 :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<ImageButton
android:id="@+id/title"
android:layout_width="250dp"
android:layout_height="58dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin ="25dp"
android:background="@drawable/skintonetitle" />
<custombuttons.CustomImageButton
android:id="@+id/skina1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/title"
android:layout_below="@+id/title"
android:layout_marginTop="35dp"
android:background="@drawable/test_circle"
android:clickable="true"
android:focusable="true" />
</RelativeLayout>
뭔가 내가 놓친 적이
여기 내 코드입니까?
아 : 당신이 자바를 수행하려는 경우
는 또한 View 클래스의 생성자에서 다음을 설정할 수 있습니다. 일을 끝내고 해보겠습니다. 내 사용자 정의 단추 클래스에서보기를 기본적으로 클릭 가능하고 포커스 가능하게 만들 수 있습니까? –
넵 - 내 대답에 그 두 줄은 XML 특성입니다. 당신은 android : background, android : id와 같은
아, 미안 해요. 사용자 정의 버튼 src의 메소드를 재정의하여 기본적으로 클릭 할 수 있도록 할 수 있습니다. 그래서 각 xml에 줄을 추가 할 필요가 없습니다. –