简体   繁体   English

android中的spanableString的背景颜色

[英]Background color to spanableString in android

I am using only one text view and using spanable to add fonts and color to strings. 我只使用一个文本视图并使用spanable为字符串添加字体和颜色。 But i am getting problem to display rounded background to string using spanable. 但我有问题,使用spanable显示圆形背景到字符串。 So How can I achieve this using spannable String concept the image is as shown below. 那么如何使用spannable String概念实现这一目标,图像如下所示。

在此输入图像描述

For the background color of your TextView you can use 对于TextView的背景颜色,您可以使用

String myString = "myString";
Spannable spanna = new SpannableString(myString);
spanna.setSpan(new BackgroundColorSpan(0xFFCCCCCC),0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);           
myTextView.setText(spanna);

and for rounded text-view create your custom XML and set background to that textview. 对于舍入的文本视图,创建自定义XML并将背景设置为该textview。

Edit 编辑

Create one XML called rounded_corner.xml and set this content 创建一个名为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>

and then add this line to your textview in XML 然后将此行添加到XML中的textview

android:background="@drawable/rounded_corner"

I have achieved the above UI like this.. Created Horizontal scroll-view and one Linear Layout with horizontal orientation. 我已经实现了上面这样的UI ..创建了水平滚动视图和一个水平方向的线性布局。 next during run-time depending upon the No of Strings in array i have added text view in Linear Layout that's it. 接下来在运行期间取决于数组中的字符串数量,我在线性布局中添加了文本视图。 The code is as below. 代码如下。

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

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

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