您的位置:首页 > 其它

切换同一个位置上的图片setCompoundDrawables

2016-08-02 09:49 316 查看
需求:

要在同一个位置上,如右上角,在不同的界面上,显示不同图片。

描述:

右上角,出现保存按钮(勾√)和添加按钮(+),在保存界面出现+,在提交界面出现√。

实现:

public void getRightChangeBtn(String str) {
Drawable drawable;

if (str.equals(RIGHT_SUBMIT)) {
drawable = getResources().getDrawable(R.drawable.btn_true);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
fbSave.setCompoundDrawables(null, null, drawable, null);
} else if (str.equals(RIGHT_ADD)) {
drawable = getResources().getDrawable(R.drawable.btn_add);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
fbSave.setCompoundDrawables(null, null, drawable, null);
} else if (str.equals(RIGHT_SAVE)) {
drawable = getResources().getDrawable(R.drawable.btn_true);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
fbSave.setCompoundDrawables(null, null, drawable, null);
}else if (str.equals(RIGHT_NULL)) {
fbSave.setVisibility(View.INVISIBLE);
}
fbSave.setWidth(fbSave.getWidth());

fbSave.setPadding(fbSave.getPaddingLeft(), fbSave.getPaddingTop(), fbSave.getPaddingRight(), fbSave.getPaddingBottom());
}


特别注意,要设置边界

drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界


如果不设置边界,图片会变形。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐