android – 如何添加角半径和填充到多行spannable文本
作者:互联网
如何在下面的可跨越文本中添加角半径和填充?
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
setFont();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setFont();
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFont();
}
private void setFont() {
Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/TEXT.ttf");
setTypeface(font, Typeface.NORMAL);
Spannable myspan = new SpannableString(getText());
myspan.setSpan(new BackgroundColorSpan(0xFF757593), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtview.setText(myspan);
}
}
解决方法:
编辑
你想要的是这样的吗?
screenshot http://i60.tinypic.com/28mogu8.png
如果是这样,您可以使用以下代码创建它:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#999999"
android:gravity="center">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:gravity="center"
android:background="@drawable/rounded_corners"/>
</LinearLayout>
这就是rounded_corners.xml文件的样子:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp"/>
<solid android:color="@android:color/white"/>
</shape>
这是MainActivity:
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/TEXT.ttf");
TextView textView = (TextView) findViewById(R.id.textView);
textView.setTypeface(font, Typeface.NORMAL);
String text = "Text1\nText2\nText3";
Spannable myspan = new SpannableString(text);
myspan.setSpan(new BackgroundColorSpan(0xFF757593), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(myspan);
}
}
我不得不使用不同的颜色,以便你可以看到它是圆角.您可以使用颜色值.我没有TEXT.ttf文件,所以我不知道它会是什么样子.上面的图片当然没有自定义字体.
标签:android,textview,spannablestring 来源: https://codeday.me/bug/20190702/1358167.html