本文介绍了Android中spanableString的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仅使用一个文本视图,并使用spanable为字符串添加字体和颜色.但是我越来越无法使用spanable将圆形背景显示为字符串.因此,如何使用Spanable String概念实现此目标,图像如下所示.

I am using only one text view and using spanable to add fonts and color to strings. But i am getting problem to display rounded background to string using spanable. So How can I achieve this using spannable String concept the image is as shown below.

推荐答案

我已经实现了上述UI..创建了水平滚动视图和一个水平方向的线性布局.接下来在运行时,根据数组中的字符串数,我已经在线性布局中添加了文本视图.代码如下.

I have achieved the above UI like this.. Created Horizontal scroll-view and one Linear Layout with horizontal orientation. 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);
         }
    }
}

这篇关于Android中spanableString的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 17:14