我有一个带有集中式icon+text的按钮,当某个事件发生时,我需要更改app:icon和android:text属性。我知道有setText()方法可以改变文本,但是有没有办法改变图标呢?
XML:
<Button
android:id="@+id/bottom_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginHorizontal="@dimen/default_margin"
android:layout_marginVertical="@dimen/bottom_bar_content_vertical_margin"
android:backgroundTint="@color/light_green"
android:text="@string/next"
android:textAllCaps="false"
app:icon="@drawable/ic_baseline_next_plan_24"
app:iconGravity="textStart" />这是一个在适当的事件之后调用的函数,我需要更改该函数中的图标。icon和text是期望的drawable和string的ids:
private void setBottomButton(int icon, int text) {
button.setText(getString(text));
}发布于 2021-02-02 22:08:32
发布于 2021-02-02 22:11:07
如果查看文档,您将看到每个XML属性的等效代码。
查看此链接:搜索drawableLeft的enter link description here显示:
android:drawableLeft:
setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable)https://stackoverflow.com/questions/66011337
复制相似问题