gpt4 book ai didi

android - 是否可以通过编程方式设置 drawableLeft?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:51:39 25 4
gpt4 key购买 nike

在 XML 中,我们可以使用这种方式设置 drawableLeft:

    <Button
android:id="@+id/previewBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/white_btn"
android:drawableLeft="@drawable/green_circle"
android:drawablePadding="16dp"
android:text="Button" />

如何以编程方式做同样的事情?

最佳答案

是的,使用 setCompoundDrawablesWithIntrinsicBounds

并为第一个参数定义可绘制对象,然后为所有其他参数定义 0。

代码应该是这样的:

Button b = findViewById(R.id.myButton);

b.setCompoundDrawablesWithIntrinsicBounds(R.drawable.myDrawable, 0, 0, 0);

如果您的可绘制对象也是在代码中创建的,那么您需要使用另一个 setCompoundDrawablesWithIntrinsicBounds该方法采用 4 个可绘制对象,并为除左侧以外的所有对象传递 null。

关于android - 是否可以通过编程方式设置 drawableLeft?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10506220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com