简体   繁体   English

更改部分textview的背景颜色

[英]Change background color of part of textview

I have a textview with three lines, for example, name, phone and address. 我有一个三行文本视图,例如,姓名,电话和地址。 I want to distinguish third line, I want to add image there, get a round border for it and change background color. 我想区分第三行,我想在那添加图像,为其添加一个圆形边框并更改背景颜色。 Is there a way I can do? 有办法吗?

To change a part of the background you'll need is a Spannable. 要更改背景的一部分,您需要一个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);

If you want it clickable you can use: 如果您希望它可点击,则可以使用:

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);

Font StackOverflow answer 字体StackOverflow答案

You have to use either Spannable or you have to use Html. 您必须使用Spannable或使用HTML。 These both will work. 这些都将起作用。

Html Example: HTML示例:

YOURTEXTVIEW.setText(Html.fromHtml("<font color='green'><b>" + YOUR BACKGROUND TEXT + "</b></font>" + "  "+ YOUR LONG LONG TEXT));

You have to just write Html for set background. 您只需要为设置背景写Html。

And for spannable you will get lots of data from Google. 而且对于跨度,您将从Google获得大量数据。

In an easy way, you can use 3 textviews like this 您可以轻松地使用3种文本视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Details" />

    <LinearLayout
        android:layout_marginLeft="15dp"
        android:orientation="vertical"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Phone" />

        <TextView
            android:background="#f00"
            android:textColor="#ffffff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Address" />
    </LinearLayout>

</LinearLayout>

Also see snapshot 另请参阅快照 在此处输入图片说明

change the back ground like this 这样改变背景

customshape.xml customshape.xml

  <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >

      <solid android:color="your_back_ground_color" />  

        <corners android:radius="8dp" />

        <stroke
            android:width="0.5dp"
            android:color="Border_color" >
        </stroke>

    </shape>

put this in res/drawable 将此放入res / drawable

in layout xml 在布局xml中

  <TextView
        android:background="@drawable/customshape"
        android:textColor="your_color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="your_text" />

and I hope if its overlaping with the textview the add padding on the custom layout. 我希望它与textview重叠时可以在自定义布局上添加填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM