如何在Android按钮上以编程方式设置drawableLeft?

407 java android android-layout android-2.2-froyo

我正在动态创建按钮.我首先使用XML设置它们,我正在尝试使用下面的XML并使其编程.

<Button
    android:id="@+id/buttonIdDoesntMatter"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:text="buttonName"
    android:drawableLeft="@drawable/imageWillChange"
    android:onClick="listener"
    android:layout_width="fill_parent">
</Button>
Run Code Online (Sandbox Code Playgroud)

这就是我到目前为止所拥有的.除了可绘制之外,我可以做任何事情.

linear = (LinearLayout) findViewById(R.id.LinearView);
Button button = new Button(this);
button.setText("Button");
button.setOnClickListener(listener);
button.setLayoutParams(
    new LayoutParams(
        android.view.ViewGroup.LayoutParams.FILL_PARENT,         
        android.view.ViewGroup.LayoutParams.WRAP_CONTENT
    )
);      

linear.addView(button);
Run Code Online (Sandbox Code Playgroud)

Var*_*run 984

您可以使用该setCompoundDrawables方法执行此操作.请参阅此处的示例.我使用它而不使用它setBounds并且它起作用.你可以尝试任何一种方式.

更新:在此处复制代码会导致链接断开

Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
img.setBounds( 0, 0, 60, 60 );
txtVw.setCompoundDrawables( img, null, null, null );
Run Code Online (Sandbox Code Playgroud)

要么

Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
txtVw.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);
Run Code Online (Sandbox Code Playgroud)

要么

txtVw.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)

  • 我在我的应用程序中看到一些奇怪的东西.如果我在layout.xml中定义了drawableRight,那么`setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.money,0)`不起作用.如果我在`onCreate()`中设置原始图标,则更改有效.它可能与API 19有关吗? (2认同)

Jig*_*iya 89

你也可以尝试一下

txtVw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)

  • R.drawable.smiley应该在第一个0(第一个参数)的位置而不是最后一个,因为这个方法的定义是:{public void setCompoundDrawablesWithIntrinsicBounds(int left,int top,int right,int bottom)} (4认同)

swa*_*aha 13

对我来说,它有效:

button.setCompoundDrawablesWithIntrinsicBounds(com.example.project1.R.drawable.ic_launcher, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)


ami*_*phy 12

Kotlin Version

使用以下代码片段向按钮添加一个可绘制对象:

val drawable = ContextCompat.getDrawable(context, R.drawable.ic_favorite_white_16dp)
button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
Run Code Online (Sandbox Code Playgroud)

Important Point in Using Android Vector Drawable

当您使用可绘制android vector并希望向21以下的API具有向后兼容性时,请将以下代码添加到:

在应用程序级别build.gradle

android {
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
}
Run Code Online (Sandbox Code Playgroud)

在应用程序类中:

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 你是对的,但是 `Context#.getDrawable(resId)` 已被弃用,所以使用它可能会导致一些问题。 (2认同)

gng*_*ath 11

myEdtiText.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley,0, 0, 0);
Run Code Online (Sandbox Code Playgroud)


Met*_*ete 9

如果您使用drawableStartdrawableEnddrawableTopdrawableBottom;您必须使用“ setCompoundDrawablesRelativeWithIntrinsicBounds

edittext.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.anim_search_to_close, 0)
Run Code Online (Sandbox Code Playgroud)


Sha*_*aon 8

为我工作。在右侧设置drawable

tvBioLive.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_close_red_400_24dp, 0)
Run Code Online (Sandbox Code Playgroud)


use*_*762 6

我这样做了:

 // Left, top, right, bottom drawables.
            Drawable[] drawables = button.getCompoundDrawables();
            // get left drawable.
            Drawable leftCompoundDrawable = drawables[0];
            // get new drawable.
            Drawable img = getContext().getResources().getDrawable(R.drawable.ic_launcher);
            // set image size (don't change the size values)
            img.setBounds(leftCompoundDrawable.getBounds());
            // set new drawable
            button.setCompoundDrawables(img, null, null, null);
Run Code Online (Sandbox Code Playgroud)