如何在按钮上添加图像(左)和文本

Roy*_*Lee 23 android button imagebutton

如何在按钮上添加图像(左)和文本?


例如:

在此输入图像描述

Mic*_*tha 51

android:drawableLeft="@drawable/image"在布局XML

你也可以从代码中做到这一点

Drawable icon= getContext().getResources().getDrawable( R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds( icon, null, null, null );
Run Code Online (Sandbox Code Playgroud)


i_m*_*hii 7

我们还可以通过编程方式管理drawable的大小

Drawable dr = getResources().getDrawable(R.drawable.mag_green);
dr.setBounds(0, 0, 43, 40); //Left,Top,Right,Bottom
search_bar.setCompoundDrawables(dr, null , null , null);
Run Code Online (Sandbox Code Playgroud)


Eve*_*ael 5

        android:text="Documentos"
        android:drawableLeft= "@drawable/ic_document"
Run Code Online (Sandbox Code Playgroud)