简体   繁体   English

Android文字格式

[英]Android text formatting

I need to format text like on the picture - some part of text must be in round rectangles like with shape background. 我需要设置图片上的文本格式-文本的某些部分必须为圆形矩形,例如形状背景。 Tried to use HTML formatting, but it does not work. 试图使用HTML格式,但是它不起作用。 Didn't find information how to implement it with span. 找不到有关如何使用span实施的信息。

Any ideas? 有任何想法吗? 图片

you can use your custom shape drawable and use Spannable to set it in your TextView . 您可以使用自定义形状drawable并使用SpannableTextView进行设置。

SpannableString ss = new SpannableString(your string); 
Drawable d = getResources().getDrawable(R.drawable.shape_drawable); 
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); 
ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 
textView.setText(ss); 

refer this answer for more 请参阅答案以获取更多信息

You can create a drawable and set it as background for the textview 您可以创建一个drawable并将其设置为textview的背景

here is an example : 这是一个例子:

create a shape.xml in your drawable folder 在您的可绘制文件夹中创建一个shape.xml

here is a sample of code you can put in shape.xml file 这是可以放入shape.xml文件中的代码示例

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

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

    <stroke
        android:width="1dp"
        android:color="#ffffff" />

    <size
        android:height="20dp"
        android:width="60dp" />

</shape>

You can make changes for color and corners as you need in this shape.xml 您可以在shape.xml中根据需要更改颜色和角落

then set that as a background for your text view Your layout.xml 然后将其设置为文本视图的背景Your layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shape"
        android:padding="10dp"
        android:text="@string/First_name" />

</RelativeLayout>

Here is what it looks like : 这是它的样子:

在此处输入图片说明

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

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