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

前端 未结 8 1013
名媛妹妹
名媛妹妹 2020-12-04 21:01

How to set property \"android:drawableTop\" of a button at runtime

相关标签:
8条回答
  • 2020-12-04 21:26

    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. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

    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. Use null if you do not want a Drawable there. The Drawables must already have had setBounds(Rect) called.

    0 讨论(0)
  • 2020-12-04 21:26
    Drawable top = getResources().getDrawable(R.drawable.image);
    button.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
    
    0 讨论(0)
  • 2020-12-04 21:27
            Button button = (Button) findViewById(R.id.button);
            button.setCompoundDrawables(left, top, right, bottom);
    
    0 讨论(0)
  • 2020-12-04 21:32

    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.

    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);
    }
    
    0 讨论(0)
  • 2020-12-04 21:38
    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);
    
            }
        });
    
    0 讨论(0)
  • 2020-12-04 21:38
     btn.setBackgroundResource(R.drawable.your_image_name_here);
    
    0 讨论(0)
提交回复
热议问题