【问题标题】:How to programmatically set drawableLeft on Android button?如何以编程方式在 Android 按钮上设置 drawableLeft?
【发布时间】:2011-05-29 00:08:13
【问题描述】:

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

这是我目前所拥有的。除了drawable,我什么都能做。

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

【问题讨论】:

    标签: java android android-layout android-2.2-froyo


    【解决方案1】:

    您可以使用setCompoundDrawables 方法来执行此操作。请参阅示例here。我在不使用setBounds 的情况下使用了它,并且它有效。你可以尝试任何一种方式。

    更新:复制代码以防链接失效

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

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

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

    【讨论】:

    • @Varun, @Tigger:我有一个问题:我的文件管理器在列表视图中显示文件夹,带有文本视图和文件夹图标为drawableLeft。当您单击没有读取权限的文件夹时,我在这里尝试了您的建议以设置“禁止图标”,并且它可以工作。但是,当您更改文件夹并重新加载适配器时,禁止图标仍然存在(即,drawableLeft 不会重绘)。你知道如何在不循环的情况下将notifyDataSetChanged 也应用于drawableLeft 吗?谢谢!
    • @LuisA.Florit 听起来您有一个与在数据更改时重绘 Listview 项目有关的问题 - 这与此问题或答案无关。我建议您发布问题而不是评论。
    • @Tigger:好吧,我也使用你的技巧恢复了图标,并在禁止的目录上循环。也许这比重绘所有 ListView 项目要好...谢谢!
    • 我在我的应用程序中看到了一些奇怪的东西。 setCompoundDrawablesWithIntrinsicBounds( 0, 0, R.drawable.money, 0 ) 不起作用,如果我在 layout.xml 中定义 drawableRight。如果我在onCreate() 中设置原始图标,那么更改将起作用。会不会和 API 19 有关?
    • 示例链接未打开。有其他链接吗?
    【解决方案2】:

    你也可以试试这个

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

    【讨论】:

    • R.drawable.smiley 应该在第一个 0(第一个参数)的位置而不是最后一个,因为这个方法的定义是:{public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int对,int 底部)}
    • 我怎样才能在这个周围添加填充呢?这样,可绘制对象和文本之间没有太多填充。
    【解决方案3】:

    Kotlin Version

    使用下面的 sn-p 向按钮左侧添加可绘制对象:

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

    .

    Important Point in Using Android Vector Drawable

    当您使用android矢量drawable并希望向后兼容API低于21时,添加以下代码:

    在应用层build.gradle

    android {
        defaultConfig {
            vectorDrawables.useSupportLibrary = true
        }
    }
    

    在应用程序类中:

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

    【讨论】:

    • 一个更简单的方法也适用于我:button.setCompoundDrawablesWithIntrinsicBounds(getDrawable(R.drawable.ic_favorite_white_16dp), null, null, null)
    • 你是对的,但Context#.getDrawable(resId) 已被弃用,因此使用它可能会导致一些问题。
    • 适用于不添加应用程序类,因为我没有。
    【解决方案4】:
    myEdtiText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley, 0, 0, 0);
    

    【讨论】:

      【解决方案5】:

      对我来说,它有效:

      button.setCompoundDrawablesWithIntrinsicBounds(com.example.project1.R.drawable.ic_launcher, 0, 0, 0);
      

      【讨论】:

        【解决方案6】:

        如果您正在使用 drawableStartdrawableEnddrawableTopdrawableBottom;你必须使用“setCompoundDrawablesRelativeWithIntrinsicBounds

        edittext.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.anim_search_to_close, 0)
        

        【讨论】:

          【解决方案7】:

          为我工作。在右侧设置drawable

          tvBioLive.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_close_red_400_24dp, 0)
          

          【讨论】:

            【解决方案8】:

            我这样做了:

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

            【讨论】:

              【解决方案9】:

              正如@Jérémy Reynaud 指出的那样,正如answer 中所述,在不更改其他可绘制对象(顶部、右侧和底部)的值的情况下设置左侧可绘制对象的最安全方法是使用带有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都会被保留。

              【讨论】:

                【解决方案10】:

                可能会有所帮助:

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

                【讨论】:

                  【解决方案11】:

                  添加 Kotlin 扩展

                  如果您要经常这样做,添加一个扩展可以让您的代码更具可读性。按钮扩展 TextView;如果您想更窄,请使用 Button。

                  fun TextView.leftDrawable(@DrawableRes id: Int = 0) {
                      this.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0)
                  }
                  

                  要使用扩展,只需调用

                  view.leftDrawable(R.drawable.my_drawable)
                  

                  任何时候你需要清除,不要传递参数或做另一个名为removeDrawables的扩展

                  【讨论】:

                    【解决方案12】:

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

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

                    【讨论】:

                      【解决方案13】:

                      试试这个:

                      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                           fillButton[i].setBackground(getBaseContext().getResources().getDrawable(R.drawable.drawable_name));
                      }
                      else {
                          fillButton[i].setBackgroundColor(Color.argb(255,193,234,203));
                      }
                      

                      【讨论】:

                        【解决方案14】:

                        试试这个:

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

                        【讨论】:

                        • myEdtiText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley,0, 0, 0);作品
                        猜你喜欢
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2015-01-09
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2019-05-20
                        相关资源
                        最近更新 更多