需求:
要在同一个位置上,如右上角,在不同的界面上,显示不同图片。
描述:
右上角,出现保存按钮(勾√)和添加按钮(+),在保存界面出现+,在提交界面出现√。
实现:
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()); //设置边界
如果不设置边界,图片会变形。
本文介绍了一种在不同界面中同一位置动态切换按钮图标的方法。通过判断当前界面类型,选择合适的图片资源并设置到按钮上,实现了保存、添加等操作的图标显示。特别注意的是,为了保持图标形状不被拉伸变形,需要正确设置图片的边界。

5931

被折叠的 条评论
为什么被折叠?



