Align drawable image inside a text view to center

前端 未结 6 1383
挽巷
挽巷 2021-02-19 11:58

I have a textview which holds an image and some text.

programmatically i have added the drawable image to the left side of text view

txtIDFav.setCompound         


        
6条回答
  •  一生所求
    2021-02-19 12:25

    Embed the drawable in your text by using a SpannableString.

            SpannableStringBuilder textspan = new SpannableStringBuilder("  " + yourText);
            Drawable icon = getResources().getDrawable(R.drawable.fav_enabled);
            // jump drawable to the text view's state (if it is a state drawable)
            icon.setState(textView.getBackground().getState());  // match the background state
            textspan.setSpan(new ImageSpan(icon, ImageSpan.ALIGN_BOTTOM), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(textspan);
    

提交回复
热议问题