更改textview部分的背景颜色

Ask*_*tov 1 android textview

我有三行文本视图,例如姓名,电话和地址.我想区分第三行,我想在那里添加图像,为它获得一个圆形边框并更改背景颜色.我有办法吗?

dio*_*jme 7

要更改背景的一部分,您需要的是Spannable.

int startColor = 0; //the size that start the background color
int endColor = 100; //the size that ends the background color

TextView textView = (TextView) findViewById(R.id.text_view_id);

Spannable spannable = new SpannableString(" Handle action bar item clicks here. The action bar will automatically handle clicks on the Home");

spannable.setSpan(new BackgroundColorSpan(Color.BLUE), startColor, endColor, 
                  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Run Code Online (Sandbox Code Playgroud)

如果您想要它可点击,您可以使用:

spannable.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "link clicked", Toast.LENGTH_SHORT).show();
        }
    }, startColor, endColor, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Run Code Online (Sandbox Code Playgroud)

Font StackOverflow回答