首页 文章

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

提问于
浏览
347

我正在动态创建按钮 . 我首先使用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>

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

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);

10 回答

  • 74

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

    UPDATE :在此处复制代码,导致链接断开

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

    or

    Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
    txtVw.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);
    

    or

    txtVw.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);
    
  • 1

    你也可以尝试一下

    txtVw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley, 0, 0, 0);
    
  • -6

    对我来说,它有效:

    button.setCompoundDrawablesWithIntrinsicBounds(com.example.project1.R.drawable.ic_launcher, 0, 0, 0);
    
  • 3
    myEdtiText.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley,0, 0, 0);
    
  • 0

    我这样做了:

    // 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);
    
  • 13

    正如@JérémyReynaud指出的那样,如_1144139_中所述,设置左绘图而不更改其他绘图(顶部,右侧和底部)的值的最安全方法是使用setCompoundDrawablesWithIntrinsicBounds按钮中的先前值:

    Drawable leftDrawable = getContext().getResources()
                              .getDrawable(R.drawable.yourdrawable);
    
    // Or use ContextCompat
    // Drawable leftDrawable = ContextCompat.getDrawable(getContext(),
    //                                        R.drawable.yourdrawable);
    
    Drawable[] drawables = button.getCompoundDrawables();
    button.setCompoundDrawablesWithIntrinsicBounds(leftDrawable,drawables[1],
                                                   drawables[2], drawables[3]);
    

    所以你以前的所有drawable都将被保留下来 .

  • 1

    可能会有所帮助:

    TextView location;
    location=(TextView)view.findViewById(R.id.complain_location);
    //in parameter (left,top,right,bottom) any where you wnat to put
    location.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.arrow,0);
    
  • 850

    以下是更改编辑文本中左侧图标的颜色并将其设置在左侧的方法 .

    Drawable img = getResources().getDrawable( R.drawable.user );
    img.setBounds( 0, 0, 60, 60 );
    mNameEditText.setCompoundDrawables(img,null, null, null);
    
    int color = ContextCompat.getColor(this, R.color.blackColor);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        DrawableCompat.setTint(img, color);
    
    } else {
        img.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
    }
    
  • 1

    ***** Kotlin版本*****

    使用下面的代码片段向按钮添加一个drawable:

    val drawable = ContextCompat.getDrawable(context, R.drawable.ic_favorite_white_16dp)
    button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
    

    .

    使用Android Vector Drawable的重点

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

    在应用级别 build.gradle

    android {
        defaultConfig {
            vectorDrawables.useSupportLibrary = true
        }
    }
    

    在Application类中:

    class MyApplication : Application() {
    
        override fun onCreate() {
            super.onCreate()
    
            AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
        }
    
    }
    
  • 10

    试试这个:

    ((Button)btn).getCompoundDrawables()[0].setAlpha(btn.isEnabled() ? 255 : 100);
    

相关问题