我正在扩展“按钮”来创建一个自定义按钮,我正在添加额外的功能 - 目前,背景可绘制不会改变触摸。以下是一些示例代码,显示了我目前正在做的事情:
/src/CustomButton.java
public class CustomButton extends Button {
public CustomButton(final Context context) {
this(context, null);
}
public CustomButton(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomButton(final Context context, final AttributeSet attrs,
final int defStyle) {
super(context, attrs, defStyle);
}
}
/res/layout/MyView.xml
<com.blah.controls.CustomButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/b_gradient_states"
android:text="Button" />
/ RES /抽拉/ b_gradient_states
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/b_gradient_pressed"
android:state_pressed="true" />
<item
android:drawable="@drawable/b_gradient"
android:state_pressed="false" />
</selector>
**注意**如果我改变
<com.blah.controls.CustomButton...
到
<Button...
触摸状态按预期工作......
答案 0 :(得分:0)
Pskink在问题评论中说:
为什么在ctor(Contexr)中你调用super(Context,null)和in ctor(Context,AttributeSet)你使用super(Context,AttributeSet,int)
这就是错误的......
public CustomButton(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
应该是:
public CustomButton(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
答案 1 :(得分:0)
// Try This.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/b_gradient_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/b_gradient_pressed" android:state_focused="true"></item>
<item android:drawable="@drawable/b_gradient" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="@drawable/b_gradient_pressed" android:state_enabled="false"></item>
</selector>