How to have Image and Text Center within a Button

后端 未结 12 2259
感动是毒
感动是毒 2020-12-02 15:48

I want to display TEXT and Icon on a Button.

+----------------------------+
|          Icon TEXT         |
+---------------------         


        
12条回答
  •  广开言路
    2020-12-02 16:08

    How about using a SpannableString as the text with an ImageSpan?

    Button myButton = ...
    SpannableString ss = new SpannableString(" " + getString(R.string.my_button_text));
    Drawable d = getResources().getDrawable(R.drawable.myIcon);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    ImageSpan span = new ImageSpan(d, DynamicDrawableSpan.ALIGN_BOTTOM);
    ss.setSpan(span, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    myButton.setText(ss);
    

提交回复
热议问题