android顶部按钮图片,安卓动态改变button顶部图片即drawableTop属性

比如一个按钮在layout文件中设置了为灰色的图片资源

android:id="@+id/polling_end"

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="1.0"

android:background="@drawable/selector_tab"

android:drawableTop="@drawable/polling_end_gray"

android:gravity="center"

android:paddingTop="15dip"

android:singleLine="true"

android:text="@string/polling_end"

android:textColor="@color/black"

android:textSize="13sp"

android:enabled="false"

/>

而我在操作过程中想要动态改变button的drawableTop属性

通过button的setCompoundDrawables方法来设置,不过在设置Drawable对象的时候需要调用setBounds方法设置一下

下面给出 setCompoundDrawables的API doc说明

public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom)

Added in API level 1

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables must already have had setBounds(Rect) called.

Related XML Attributes

android:drawableLeft

android:drawableTop

android:drawableRight

android:drawableBottom

可以看出,顺序是左->上->右->下的,那么只要传入第二个参数就行了

@Override

public void onClick(View v) {

Drawable startDra, reportDra, endDra;

switch (v.getId()){

case R.id.goback:

getSwipeBackLayout().scrollToFinishActivity();

break;

case R.id.polling_start:

todo.setText("正在获取...");

done.setText("正在获取...");

distance.setText("正在计算...");

changeStatus("online");

pollingSwitch = true;

startDra = getResources().getDrawable(R.drawable.polling_start_gray);

startDra.setBounds(0, 0, startDra.getMinimumWidth(), startDra.getMinimumHeight());

start.setCompoundDrawables(null, startDra, null, null);

report.setEnabled(true);

reportDra = getResources().getDrawable(R.drawable.polling_report);

reportDra.setBounds(0, 0, reportDra.getMinimumWidth(), reportDra.getMinimumHeight());

report.setCompoundDrawables(null, reportDra, null, null);

end.setEnabled(true);

endDra = getResources().getDrawable(R.drawable.polling_end);

endDra.setBounds(0, 0, endDra.getMinimumWidth(), endDra.getMinimumHeight());

end.setCompoundDrawables(null, endDra, null, null);

MyUtils.ToastMsg(this, "开始巡检");

break;

case R.id.polling_report:

if(lastLocation == null){

MyUtils.ToastMsg(this, "暂无定位信息,请稍后再试");

return;

}

pollingSwitch = false;

Intent intent = new Intent(PollingActivity.this, ReportActivity.class);

intent.putExtra("lon", lastLocation.getLongitude());

intent.putExtra("lat", lastLocation.getLatitude());

intent.putExtra("alt", Double.parseDouble(new DecimalFormat("#.00").format(lastLocation.getAltitude())));

PollingActivity.this.startActivity(intent);

PollingActivity.this.overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);

break;

case R.id.polling_end:

changeStatus("offline");

pollingSwitch = false;

startDra = getResources().getDrawable(R.drawable.polling_start);

startDra.setBounds(0, 0, startDra.getMinimumWidth(), startDra.getMinimumHeight());

start.setCompoundDrawables(null, startDra, null, null);

report.setEnabled(true);

reportDra = getResources().getDrawable(R.drawable.polling_report_gray);

reportDra.setBounds(0, 0, reportDra.getMinimumWidth(), reportDra.getMinimumHeight());

report.setCompoundDrawables(null, reportDra, null, null);

end.setEnabled(true);

endDra = getResources().getDrawable(R.drawable.polling_end_gray);

endDra.setBounds(0, 0, endDra.getMinimumWidth(), endDra.getMinimumHeight());

end.setCompoundDrawables(null, endDra, null, null);

MyUtils.ToastMsg(this, "结束巡检");

break;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值