之前想到的一直是自定义,然而水平有限,只能先查看api,找到一个属性drawableBottom属性还有什么drawableLeft等属性,而且是设置图片,不管怎么样设置一下。然后居然跟需要的效果一样。赶紧记录下
布局文件中
<Button
android:id="@+id/air_condition_smart_btn"
android:layout_width="65dp"
android:layout_height="65dp"
android:text="16c"
android:textSize="16sp"
android:textColor="#000000"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp" android:layout_gravity="center_vertical" android:drawableBottom="@drawable/condition_mode_auto_green" android:background="@drawable/condition_smart_btn_back"
/>
等等,在代码中怎么动态修改这个图片呢。这个图片是需要根据具体数据来更改的。
代码中可没有这个属性,然后果断找到setCompoundDrawables这个方法。百度了下这个方法使用方式,记录下
Drawable modeImage;
Resources res = AirBoxDetailActivity.context.getResources();
modeImage = res.getDrawable(R.drawable.condition_mode_auto_green);
modeImage = res.getDrawable(R.drawable.condition_mode_auto_green);
modeImage.setBounds(0, 0, modeImage.getMinimumWidth(), modeImage.getMinimumHeight());
smart_condition_btn.setCompoundDrawables(null, null, null, modeImage);
这样就达到了,根据情况选择不同的图片来替换button上小图标的目的了