首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何以编程方式更改Button应用程序:图标属性

如何以编程方式更改Button应用程序:图标属性
EN

Stack Overflow用户
提问于 2021-02-02 22:06:29
回答 2查看 64关注 0票数 0

我有一个带有集中式icon+text的按钮,当某个事件发生时,我需要更改app:iconandroid:text属性。我知道有setText()方法可以改变文本,但是有没有办法改变图标呢?

XML:

代码语言:javascript
运行
复制
<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" />

这是一个在适当的事件之后调用的函数,我需要更改该函数中的图标。icontext是期望的drawable和string的ids:

代码语言:javascript
运行
复制
private void setBottomButton(int icon, int text) {
    button.setText(getString(text));
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-02 22:08:32

您可以使用以下方法:

代码语言:javascript
运行
复制
setIcon
setIconResource

示例:

代码语言:javascript
运行
复制
button.setIcon(drawable)
button.setIconResource(iconId)

这里是doc

票数 1
EN

Stack Overflow用户

发布于 2021-02-02 22:11:07

如果查看文档,您将看到每个XML属性的等效代码。

查看此链接:搜索drawableLeft的enter link description here显示:

代码语言:javascript
运行
复制
android:drawableLeft:
setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66011337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档