【问题标题】:How do I show text in my TextView with bold and multi coloured如何在我的 TextView 中以粗体和多色显示文本
【发布时间】:2013-01-21 12:13:07
【问题描述】:

我的要求是在 TextView 中以彩虹色文本和粗体等多色显示文本,我该如何实现这一点。我需要使用 java 代码动态显示它们..

TextView text=new TextView(context);
                        text.setText(status);
                    text.setBackgroundResource(R.drawable.grd_btn);
                    text.setGravity(Gravity.CENTER);
                    text.setPadding(2, 0, 2, 0);
                    text.setTypeface(font2,Typeface.BOLD);
                    text.setTextColor(Color.WHITE);

【问题讨论】:

标签: java android android-layout textview


【解决方案1】:

抱歉,我的朋友耽搁了。必须解决您的问题,这需要一段时间。所以首先是输出,

所以假设上面显示的是您需要的输出,这里是它的代码。

xml 文件

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world"/>

res 文件(比如 strings.xml)

<color  name="violet">#9400D3</color>
<color  name="indigo">#4B0082</color>
<color  name="blue">#0000FF</color>
<color  name="green">#00FF00</color>
<color  name="yellow">#FFFF00</color>
<color  name="orange">#FF7F00</color>
<color  name="red">#FF0000</color>

您的 java 文件

     TextView textView = (TextView)findViewById(R.id.textView1);
        Shader textShader=new LinearGradient(0, 0, 0, 20,
                new int[]{getResources().getColor(R.color.violet),getResources().getColor(R.color.indigo),
                getResources().getColor(R.color.blue),
                getResources().getColor(R.color.green),
                getResources().getColor(R.color.yellow),
                getResources().getColor(R.color.orange),
                getResources().getColor(R.color.red)},
                new float[]{0,0.2f,0.4f,0.6f,0.8f,0.9f,1}, TileMode.CLAMP);
        textView.getPaint().setShader(textShader);
        textView.setTextSize(20);

就是这样。对于您的大胆风格,请点击我之前回答的以下链接,

https://stackoverflow.com/a/5169604/603744

【讨论】:

  • 在 API 23(Android M) 上尝试此代码,它不起作用。它显示一种颜色。
【解决方案2】:
 String text = "This is <font color='red'>red</font>. This is <font     color='blue'>blue</font>.";
 textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);
 textView.setTypeface(null,Typeface.BOLD);

【讨论】:

    【解决方案3】:

    这对我来说最简单

    所有致谢: https://gist.github.com/ishitcno1/0c8bcb8ad72cb0879acb

    public class RainbowTextView extends TextView {
      protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    
        int[] rainbow = getRainbowColors();
        Shader shader = new LinearGradient(0, 0, 0, w, rainbow, 
            null, Shader.TileMode.MIRROR);
    
        Matrix matrix = new Matrix();
        matrix.setRotate(90);
        shader.setLocalMatrix(matrix);
    
        getPaint().setShader(shader);
      }
    
      private int[] getRainbowColors() {
        return new int[] {
          getResources().getColor(R.color.rainbow_red),
          getResources().getColor(R.color.rainbow_yellow),
          getResources().getColor(R.color.rainbow_green),
          getResources().getColor(R.color.rainbow_blue),
          getResources().getColor(R.color.rainbow_purple)
        };
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-06
      • 2013-02-10
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      相关资源
      最近更新 更多