简体   繁体   English

如何在运行时设置按钮的属性“android:drawableTop”

[英]How to set property “android:drawableTop” of a button at runtime

如何在运行时设置按钮的属性“ android:drawableTop

Use 使用

button.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. 将Drawables(如果有)设置为显示在文本的左侧,上方,右侧和下方。 Use 0 if you do not want a Drawable there. 如果你不想在那里使用Drawable,请使用0。 The Drawables' bounds will be set to their intrinsic bounds. Drawables的界限将设置为其内在界限。

If you use 如果你使用

button.setCompoundDrawables(left, top, right, bottom);

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. 将Drawables(如果有)设置为显示在文本的左侧,上方,右侧和下方。 Use null if you do not want a Drawable there. 如果你不想在那里使用Drawable,请使用null。 The Drawables must already have had setBounds(Rect) called. Drawables必须已经调用了setBounds(Rect)

Drawable top = getResources().getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
final Drawable drawableTop = getResources().getDrawable(R.drawable.btn_check_buttonless_on);

btnByCust.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {


 btnByCust.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);

        }
    });
        Button button = (Button) findViewById(R.id.button);
        button.setCompoundDrawables(left, top, right, bottom);

I use this code for use the "Theme.Holo" button with a "Custom image" at left and change it (the image)with a function that is called from various ways. 我使用此代码使用左侧带有“自定义图像”的“Theme.Holo”按钮,并使用从各种方式调用的函数更改它(图像)。

protected void app_dibujarLogojuego() {
    if(bitmaplogojuego!=null){
        bitmaplogojuego.recycle();
        bitmaplogojuego=null;
    }
    Drawable LOGO = null;
    if(verjuego.equals("COSA1")){  LOGO = getResources().getDrawable(R.drawable.img_logo_COSA1);  }
    if(verjuego.equals("COSA2")){  LOGO = getResources().getDrawable(R.drawable.img_logo_COSA2);  }
    if(verjuego.equals("COSA3")){  LOGO = getResources().getDrawable(R.drawable.img_logo_COSA3);  }
    if(verjuego.equals("COSA4")){  LOGO = getResources().getDrawable(R.drawable.img_logo_COSA4);  }

    BUTTON_DECLARED_ID.setCompoundDrawablesWithIntrinsicBounds(LOGO, null , null, null);
}
 btn.setBackgroundResource(R.drawable.your_image_name_here);

If you are using Kotlin, you can use extension method to make things look elegant. 如果您使用Kotlin,您可以使用扩展方法使事物看起来很优雅。

fun TextView.setDrawableTop(iconId: Int) {
    val icon = this.context?.resources?.getDrawable(iconId)
    this.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null)
}

Then you can use it like this: 然后你可以像这样使用它:

// myTextView: TextView
myTextView.setDrawableTop(R.drawable.ic_happy)

Create an extension function like this and set top drawable like this 像这样创建一个扩展函数并像这样设置top drawable

tvAccepted.setTopDrawable(R.drawable.ic_preparing_order_active)

fun TextView.setTopDrawable(icon: Int) {
    this.setCompoundDrawablesRelativeWithIntrinsicBounds(0,icon,0,0)
}

where 哪里

setCompoundDrawablesRelativeWithIntrinsicBounds(left/start, top, right/end, bottom)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM