我只使用一个文本视图并使用spanable为字符串添加字体和颜色.但我有问题,使用spanable显示圆形背景到字符串.那么如何使用spannable String概念实现这一目标,图像如下所示.

Inn*_*ler 11
您可以使用的文本视图的冷杉背景颜色
String myString = "myString";
Spannable spanna = new SpannableString(myString);
spanna.setSpan(new BackgroundColorSpan(0xFFCCCCCC),0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(spanna);
Run Code Online (Sandbox Code Playgroud)
对于舍入的文本视图,创建自定义XML并将背景设置为该textview.
编辑
创建一个名为rounded-corner.xml的 XML 并设置此内容
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- view background color -->
<solid android:color="#ffffff" >
</solid>
<!-- view border color and width -->
<stroke
android:width="1dp"
android:color="#1c1b20" >
</stroke>
<!-- If you want to add some padding -->
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" >
</padding>
<!-- Here is the corner radius -->
<corners android:radius="10dp" >
</corners>
</shape>
Run Code Online (Sandbox Code Playgroud)
然后将此行添加到XML中的textview
android:background="@drawable/rounded_corner"
Run Code Online (Sandbox Code Playgroud)
我已经实现了上面的用户界面,如下所示。创建了水平滚动视图和一个水平方向的线性布局。接下来,在运行时,根据数组中的字符串数量,我在线性布局中添加了文本视图,就是这样。代码如下。
private void addTextView(ArrayList<String> list,String whichLayout){
for (int i = 0; i < list.size(); i++) {
TextView textView = new TextView(getActivity());
Spannable spValue = new SpannableString(list.get(i).toString());
textView.setText(customeSpan.getRequiredFontTypeToText(spValue, tfHintTxtValue));
textView.setTextSize(12.0f);
textView.setTextColor(getResources().getColor(R.color.black));
textView.setBackgroundResource(R.drawable.red_solid_background);
LinearLayout.LayoutParams lllp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lllp.setMargins(0, 2, 10, 0); // llp.setMargins(left, top, right, bottom);
textView.setLayoutParams(lllp);
if(whichLayout.equalsIgnoreCase("hieghtWieght")){
((LinearLayout) heightWieghtLinearLayout).addView(textView);
}else if(whichLayout.equalsIgnoreCase("bodyType")){
((LinearLayout) bodyTypeLinearLayout).addView(textView);
}else if(whichLayout.equalsIgnoreCase("eyeHair")){
((LinearLayout) eyeHairColorLinearLayout).addView(textView);
}else if(whichLayout.equalsIgnoreCase("bestFeatures")){
((LinearLayout) bestFeaturesLinearLayout).addView(textView);
}else if(whichLayout.equalsIgnoreCase("personalStyle")){
((LinearLayout) personalStyleLinearLayout).addView(textView);
}else if(whichLayout.equalsIgnoreCase("zodiacSign")){
((LinearLayout) zodizcSignLinearLayout).addView(textView);
}else if(whichLayout.equalsIgnoreCase("personalityTraits")){
((LinearLayout) personalityLinearLayout).addView(textView);
}
}
}
Run Code Online (Sandbox Code Playgroud)