java - 如何根据android中显示的字母更改字母的样式

标签 java android android-studio

我想在有 A、B、C、D、E .... 的地方更改粗体上的样式字母并更改背景。 有可能是因为我试图(注释掉行),但总是有些东西不起作用。

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {

        gridView = new View(context);

        // get layout from mobile.xml
        gridView = inflater.inflate(R.layout.mobile, null);

        int y = position / 11;
        int x = position % 11;

        //if(x<x+11 || x> plansza.length || y<0 || y> plansza[x].length);

        // set value into textview
        TextView textView = (TextView) gridView
                .findViewById(R.id.grid_item_label);

        textView.setText((x > 0 && y > 0) ? plansza[x-1][y-1].toString().substring(0,1): mobileValues[position]);

        // set image based on selected text
        ImageView imageView = (ImageView) gridView
                .findViewById(R.id.grid_item_image);

        String mobile = mobileValues[position];
        //Spannable spn = (Spannable) textView.getText();
        SpannableString spannable = new SpannableString(mobile);

        if (mobile.equals("A")) {
            //spannable.setSpan(new ForegroundColorSpan(Color.RED), 0, mobile.length(), 0);
            //spn.setSpan(new BackgroundColorSpan(Color.RED), 0, 7,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            //spn.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            //imageView.setImageResource(R.drawable.windows_logo);
        }
        else if (mobile.equals("B")) {
            //imageView.setImageResource(R.drawable.ios_logo);
        }
        else if (mobile.equals("C")) {
            //imageView.setImageResource(R.drawable.blackberry_logo);
        }
        else if (mobile.equals("D")) {
            //imageView.setImageResource(R.drawable.blackberry_logo);
        }
        else if (mobile.equals("E")) {
            //imageView.setImageResource(R.drawable.blackberry_logo);
        }
        else if (mobile.equals("F")) {
            //imageView.setImageResource(R.drawable.blackberry_logo);
        }
        else if (mobile.equals("G")) {
            //imageView.setImageResource(R.drawable.blackberry_logo);
        }
        else if (mobile.equals("H")) {
            //imageView.setImageResource(R.drawable.blackberry_logo);
        }
        else if (mobile.equals("I")) {
            //imageView.setImageResource(R.drawable.blackberry_logo);
        }
        else if (mobile.equals("J")) {
            //imageView.setImageResource(R.drawable.blackberry_logo);
        }else {
            //imageView.setImageResource(R.drawable.android_logo);
        }

    } else {
        gridView = (View) convertView;
    }

    return gridView;
}

最佳答案

Spannable wordtoSpan = new SpannableString("A");        

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

textView.setText(wordtoSpan);

关于java - 如何根据android中显示的字母更改字母的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34961260/

相关文章:

android - 关于安卓的多语言应用?

android - AndroidManifest.xml 错误 "must have a minimum of 2 segments"

添加 mopub sdk 时出现 Android Studio 错误

android - 在 Android Studio 中添加外部库

java - Intellij IDEA Apache Tomcat。如何编辑图像?

java - MySQL/SQL 增加一列

java - 运行应用程序时的 IntelliJ MyClass.class(没有这样的文件或目录)

android - 更新 android studio 3.1 后获取 'TransactionTooLargeException'

java - Spring 安全 : session-config with COOKIE not working

java - 如何在一行中将字符串数组转换为 double 组