比如一个按钮在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;
}
}