java不显示按钮的值,自定义按钮上的自定义属性不显示

我扩展了Button小部件,以便能够应用多个自定义属性 .

其中一个属性是我在创建按钮时尝试应用于其背景的滤镜 . 这是行不通的 . (参见下面的屏幕截图和代码)

我尝试在相同的代码位置直接设置背景颜色,它确实改变了背景颜色,但它不是我需要的,因为我使用自己的按钮PNG .

到目前为止有2个问题:

未应用滤色器

自定义按钮已偏移,剪裁且无法单击

第二个按钮使用普通按钮,它按预期定位,可以点击 . 第二个屏幕截图显示确实选择了正确的颜色,并且可以更改代码中该点的按钮背景颜色 .

码:

public class MyButton extends Button {

private int backGroundColor;

public MyButton(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

TypedArray a = context.getTheme().obtainStyledAttributes(

attrs,

R.styleable.MyButton,

defStyle,

0);

try {

Resources res = getResources();

switch( a.getInteger(R.styleable.MyButton_type, 0) ) {

case 0:

backGroundColor = res.getColor(R.color.Black); break;

case 1:

backGroundColor = res.getColor(R.color.Red); break;

case 2:

backGroundColor = res.getColor(R.color.DimGray); break;

}

getBackground().setColorFilter(backGroundColor, Mode.MULTIPLY);

//setBackgroundColor(backGroundColor)

} finally {

a.recycle();

}

}

public MyButton(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public MyButton(Context context) {

this(context, null, 0);

}

}

我使用的XML:

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/btn1"

android:text="BTN1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:textSize="14sp"

android:textColor="@color/Blue"

android:padding="2dp"

android:layout_margin="4dp"

android:background="@drawable/key_selector"

app:type="RedButton"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:textSize="14sp"

android:textColor="@color/Blue"

android:padding="2dp"

android:layout_margin="4dp"

android:background="@drawable/key_selector"

android:id="@+id/btn2"

android:text="BTN2"/>

setColorFilter()结果

KGs75.png

的屏幕截图

setBackgroundColor()的截图结果

UB9L4.png

EDIT 这是我用于普通和按下状态的选择器XML .

android:drawable="@drawable/key1_pressed"

android:state_pressed="true"/>

android:drawable="@drawable/key1"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值