Android 自定义视图不响应触摸
我已经创建了一个自定义视图,当按下、高亮显示或禁用时,该视图应该会更改其背景图像。应用程序运行,但按钮不会改变其背景 这是我的密码:Android 自定义视图不响应触摸,android,android-layout,android-widget,custom-controls,touch,Android,Android Layout,Android Widget,Custom Controls,Touch,我已经创建了一个自定义视图,当按下、高亮显示或禁用时,该视图应该会更改其背景图像。应用程序运行,但按钮不会改变其背景 这是我的密码: public class CustomImageButton extends View { public CustomImageButton(Context context) { super(context); setFocusable(true); setClickable(true); } public CustomImageButton(Context
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:
<?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>
我错过了什么?它从视图扩展,而不是按钮,因此默认情况下它不可单击或聚焦。调整
android:clickable="true"
android:focusable="true"
在XML中
如果您想在java中进行设置,还可以在视图类的构造函数中设置:
setFocusable(true);
setClickable(true);
在我的例子中,我使用了一个带有约束布局的自定义视图作为根视图。我没有在自定义视图的setOnClickListener上获得单击事件,结果我需要在自定义视图的xml根中设置
android:clickable=“false”
,单击事件被发送到自定义视图xml的根目录,而不是自定义视图本身(即自定义视图的setOnClickListener)Ah,这是有意义的。我下班后试试看。在我的自定义按钮类中,我可以使视图默认为可点击和可聚焦吗?是的,我的答案中的这两行是XML属性。你会把它们放在你的哦,对不起,我的意思是我可以覆盖我的自定义按钮src中的一个方法,使其在默认情况下可点击。所以我不需要在每个XML中添加行。哦,对不起。在该视图的构造函数中,您可以调用setFocusable(true)和setClickable(true)。只是对其进行了测试,但仍然不会做出反应…hmmm(抱歉)同时添加到xml和java中。编辑问题以显示更改。进行更改的代码是否有问题?