动态设置 android:drawableTop|Right|Left|Bottom
Android中有时需动态设置控件四周的drawble图片,这个时候就需要调用
setCompoundDrawables(left, top, right, bottom),
四个参数类型都是左边/顶部/右边/底部
Button继承TextView,所以可以采用相同的设置方法
方法一.XML方式
<Button
android:id="@+id/btn"
android:textColor="@color/shenghuise"
android:drawableTop="@mipmap/ic_launcher"
android:background="@color/white"
android:textSize="17sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
方法二.JAVA代码
Drawable dra;
Resources res = getResources();
dra= res.getDrawable(R.drawable. ic_launcher );
//调用setCompoundDrawables时,必须调用Drawable.setBounds()方法,否则图片不显示
dra.setBounds(0, 0, dra.getMinimumWidth(), dra.getMinimumHeight());
btn.setCompoundDrawables(null, dra, null, null); //设置顶部图标
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
2013-09-02 Android 在webView中创建web应用(译文)