How to add drawable image inside textview

前端 未结 2 893
南方客
南方客 2020-12-12 08:30

I need a drawable within the text, just like an emoji, so e.g.

\"Please press the button looking like this

相关标签:
2条回答
  • 2020-12-12 08:54

    You can try unicode:

    int unicode = 0x1F60A;
    

    And use it like this:

    public String getEmojiByUnicode(int unicode){
        return new String(Character.toChars(unicode));
    }
    
    0 讨论(0)
  • 2020-12-12 08:56

    I need a drawable within the text

    You can Use ImageSpan

    • Span that replaces the text it's attached to with a Drawable that can be aligned with the bottom or with the baseline of the surrounding text. The drawable can be constructed from varied sources:

    but with a custom drawable instead of the emoji in my example. (How) is this possible?

    Try this working example

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            TextView  textView=findViewById(R.id.tv);
            Spannable span = new SpannableString("Please press the button looking like this and then proceed ..");
            Drawable test = getResources().getDrawable(R.drawable.abc);
            test.setBounds(0, 0, 32,32);
            ImageSpan imageSpan = new ImageSpan(test, ImageSpan.ALIGN_BASELINE);
            span.setSpan(imageSpan, 36, 37, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    
            textView.setText(span);
        }
    
    
    }
    

    RESULT

    0 讨论(0)
提交回复
热议问题