【问题标题】:How to bold , chage font size , color differnt parts of a textview in android java如何在android java中加粗,更改字体大小,为textview的不同部分着色
【发布时间】:2017-06-24 03:55:13
【问题描述】:

我有一个为人们列出婴儿名字的 android 应用程序......它有一个 textview 连接不同的文本和数据字符串,然后显示它们......(婴儿名字+“意味着" + 名字的意思)

我正在尝试更改字体大小和颜色并将文本的第一个字符串(婴儿姓名)加粗,然后在其后添加一条水平线或分隔线或分隔符。

然后我想分别设置第二个文本字符串(“意思”文本)的字体大小和颜色,并将其设置为粗体。

然后我想分别设置第三个字符串的字体大小和颜色,并将其设为粗体。

我一直在阅读有关 SpannableString 的信息,并在过去 3 小时内尝试实施它,但没有成功。如果有人能提供帮助,将不胜感激!

这是我目前的工作代码

我的TextView

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/common_name_description_text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="24dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:padding="16dp">

我的 JAVA

package mypackage.android;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import mypackage.android.database.CommonNamesAdapter;

public class CommonNameDescription extends AppCompatActivity {

    String common_name;
    String common_name_meaning;
    long common_name_rowid;

    CharSequence mybreak = "\n";
    CharSequence text = "Means";
    CharSequence description;

    public static TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_names_description);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Bundle extras = getIntent().getExtras();

        common_name_rowid = extras.getLong(CommonNamesAdapter.COMMON_NAME_ROWID );
        common_name = extras.getString(CommonNamesAdapter.COMMON_NAME);
        common_name_meaning = extras.getString(CommonNamesAdapter.COMMON_NAME_MEANING).toString();


        description = common_name+mybreak+text+mybreak+common_name_meaning;
        tv = (TextView)findViewById(R.id.common_name_description_text);

        tv.setText(description);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean bRet=false;//set true is menu selection handled
        switch (item.getItemId()) {
            case R.id.action_settings_get_pro:
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(getString(R.string.pro_version_url)));
                startActivity(intent);
                bRet=true;
                break;
            case R.id.action_settings_get_pro2:
                Intent intent2 = new Intent(Intent.ACTION_VIEW);
                intent2.setData(Uri.parse(getString(R.string.pro_version_url)));
                startActivity(intent2);
                bRet=true;
                break;
            case R.id.action_settings_app_help:
                Toast.makeText(this, this.getString(R.string.action_settings_app_help_text), Toast.LENGTH_SHORT).show();
                bRet=true;
                break;
            case R.id.action_settings_about_app:
                Toast.makeText(this, this.getString(R.string.action_settings_about_text), Toast.LENGTH_SHORT).show();
                bRet=true;
                break;
            case R.id.action_settings_rate_app:
                Intent intent3 = new Intent(Intent.ACTION_VIEW);
                intent3.setData(Uri.parse(getString(R.string.rate_this_app_url)));
                startActivity(intent3);
                bRet=true;
                break;
            case R.id.action_settings_privacy_policy:
                Intent intentprivacy = new Intent(Intent.ACTION_VIEW);
                intentprivacy.setData(Uri.parse(getString(R.string.privacy_policy_url)));
                startActivity(intentprivacy);
                bRet=true;
                break;
            case R.id.action_settings_all_our_apps:
                Intent intent4 = new Intent(Intent.ACTION_VIEW);
                intent4.setData(Uri.parse(getString(R.string.all_our_apps_url)));
                startActivity(intent4);
                bRet=true;
                break;
            default:
                bRet=super.onOptionsItemSelected(item);
        }
        return bRet;
    }

}

这是我正在尝试做的一个例子
注意我知道html标签不起作用....这只是为了显示我试图将文本包装在什么以及我试图将分隔线放在哪里)

package mypackage.android;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import mypackage.android.database.CommonNamesAdapter;

public class CommonNameDescription extends AppCompatActivity {

    String common_name; < --- NEED TO CHANGE FONT SIZE AND COLOR AND MAKE BOLD
    String common_name_meaning; < --- NEED TO CHANGE FONT SIZE AND COLOR AND MAKE BOLD SEPERATLY FROM THE STRING ABOVE
    long common_name_rowid;

    CharSequence mybreak = "\n";
    CharSequence text = "Means"; < --- NEED TO CHANGE FONT SIZE AND COLOR AND MAKE BOLD SEPERATLY FROM THE OTHER 2 STRINGS ABOVE
    CharSequence description;

    public static TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_names_description);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Bundle extras = getIntent().getExtras();

        common_name_rowid = extras.getLong(CommonNamesAdapter.COMMON_NAME_ROWID );
        common_name = extras.getString(CommonNamesAdapter.COMMON_NAME);
        common_name_meaning = extras.getString(CommonNamesAdapter.COMMON_NAME_MEANING).toString();


        description = <b><font size="size here" color="color here">common_name</b></font>+horizontal_line_or_divder+<b><font size="size here" color="color here">text</b></font>+mybreak+<b><font size="size here" color="color here">common_name_meaning</b></font>;

        tv.setText(description);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean bRet=false;//set true is menu selection handled
        switch (item.getItemId()) {
            case R.id.action_settings_get_pro:
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(getString(R.string.pro_version_url)));
                startActivity(intent);
                bRet=true;
                break;
            case R.id.action_settings_get_pro2:
                Intent intent2 = new Intent(Intent.ACTION_VIEW);
                intent2.setData(Uri.parse(getString(R.string.pro_version_url)));
                startActivity(intent2);
                bRet=true;
                break;
            case R.id.action_settings_app_help:
                Toast.makeText(this, this.getString(R.string.action_settings_app_help_text), Toast.LENGTH_SHORT).show();
                bRet=true;
                break;
            case R.id.action_settings_about_app:
                Toast.makeText(this, this.getString(R.string.action_settings_about_text), Toast.LENGTH_SHORT).show();
                bRet=true;
                break;
            case R.id.action_settings_rate_app:
                Intent intent3 = new Intent(Intent.ACTION_VIEW);
                intent3.setData(Uri.parse(getString(R.string.rate_this_app_url)));
                startActivity(intent3);
                bRet=true;
                break;
            case R.id.action_settings_privacy_policy:
                Intent intentprivacy = new Intent(Intent.ACTION_VIEW);
                intentprivacy.setData(Uri.parse(getString(R.string.privacy_policy_url)));
                startActivity(intentprivacy);
                bRet=true;
                break;
            case R.id.action_settings_all_our_apps:
                Intent intent4 = new Intent(Intent.ACTION_VIEW);
                intent4.setData(Uri.parse(getString(R.string.all_our_apps_url)));
                startActivity(intent4);
                bRet=true;
                break;
            default:
                bRet=super.onOptionsItemSelected(item);
        }
        return bRet;
    }

}

【问题讨论】:

  • 可扩展字符串? stackoverflow.com/a/5169604/2308683
  • 每个示例都令人困惑,因为它们是在单个字符串上或不使用数据库,谁能给我一个像我这样的代码示例?
  • @skapaid 字符串始终是一个字符串,不管它来自哪里,你可以说 String myName = "skapaid";并将 myName 应用到您要放置字符串的位置,或者您可以简单地将“skapaid”放在要放置字符串的位置。

标签: android android-layout textview


【解决方案1】:
TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Background text");
spString.setSpan(new BackgroundColorSpan(Color.GREEN), 5, spString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);

详情请点击此链接

https://way2androidtutorials.blogspot.in/2017/08/spannablestring-is-class-its-extends.html

【讨论】:

    【解决方案2】:

    我想通了......

    调用我需要的数据库资源 String description = "&lt;b&gt;" + String.valueOf(common_name) + "&lt;/b&gt; " + String.valueOf(common_name);

    然后tv.setText(Html.fromHtml(description ));

    没有人添加String.valueOf(common_name),所以我不知道如何引用它

    【讨论】:

    • 你什么也没想到。为什么要使用 String 的 String 值 ?它将正常工作String description = "&lt;b&gt;" + common_name + "&lt;/b&gt; " + common_name;
    【解决方案3】:

    试试这个,

    CharSequence mybreak = "\n";
    CharSequence text = "Means"; 
    CharSequence description;
    
    
    Bundle extras = getIntent().getExtras();
    
    common_name_rowid = extras.getLong(CommonNamesAdapter.COMMON_NAME_ROWID );
    common_name = extras.getString(CommonNamesAdapter.COMMON_NAME);
    common_name_meaning = extras.getString(CommonNamesAdapter.COMMON_NAME_MEANING).toString();
    
    description ="<b><font size=13 color=cc0029>"+common_name+"</b></font>+horizontal_line_or_divder+<b><font size=7 color=#000000>"+text+"</b></font>"+mybreak+"<b><font size=7 color=#FF6D00>"+common_name_meaning+"</b></font>";
    
    tv.setText(Html.fromHtml((String) description));
    

    【讨论】:

    • 通过改变 CharSequence 描述得到结果;到字符串描述;它起作用了……不知道为什么……但是它现在起作用了
    【解决方案4】:

    简单

      TextView text=(TextView)findViewById(R.id.text);
            final SpannableStringBuilder str = new SpannableStringBuilder("the  baby name  means  the meaning of the nam");
            str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), STAT_CHAR_POSITION_INT_FOR_STYLE, END_CHAR_POSITION_INT_FOR_STYLE, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            text.setText(str);
    

    或自定义方法

    TextView text=(TextView)findViewById(R.id.text);
    setTextWithSpan(text,"the  baby name  means  the meaning of the nam","baby",new android.text.style.StyleSpan(android.graphics.Typeface.BOLD));
    
    
    // call this method 
      void setTextWithSpan(TextView textView, String text, String spanText, StyleSpan style) {
            SpannableStringBuilder sb = new SpannableStringBuilder(text);
            int start = text.indexOf(spanText);
            int end = start + spanText.length();
            sb.setSpan(style, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(sb);
        }
    

    编辑

    正如我所提到的,您可以使用相同的功能相同的概念!这会起作用,但可能不会像您期望的那样。所以改变它然后你就会学习。

    您需要在 Sting 中进行主要的三项更改。我的答案将在同一字符串中演示粗体、斜体和不同的字体大小。应用相同的概念并根据需要进行调整!

      void setTextWithSpan(TextView textView, String text, String spanTextBold,String secondPartItalic,String thirdPartLargeFont ,StyleSpan style) {
    
            SpannableStringBuilder sb = new SpannableStringBuilder(text);
    
            int start = text.indexOf(spanTextBold);
            int end = start + spanTextBold.length();
            sb.setSpan(style, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
            sb.setSpan(new ForegroundColorSpan(Color.BLUE), start, end ,0);
    
            int startTwo = text.indexOf(secondPartItalic);
            int endTwo = startTwo + secondPartItalic.length();
            sb.setSpan(new StyleSpan(Typeface.ITALIC),startTwo,endTwo , 0);
    
            int startThree = text.indexOf(thirdPartLargeFont);
            int endThree = startThree + thirdPartLargeFont.length();
            sb.setSpan(new RelativeSizeSpan(2.8f), startThree, endThree, 0);
    
            textView.setText(sb);
        }
    

    你可以这样称呼,

    setTextWithSpan(text,"The baby name means  the meaning of the name","baby", "means","the name",new android.text.style.StyleSpan(android.graphics.Typeface.BOLD));
    

    输出

    请参阅How to set the font style to bold, italic and underlined in an Android TextView? 了解更多属性知识。

    【讨论】:

    • 我现在迷路了.....宝宝的名字是一个变量..我认为您上面发布的方法仅适用于一个静态文本我有 2 个不同的变量和一个静态文本我试图做的事情... description = common_name+horizo​​ntal_line_or_divder+text+mybreak+common_name_meaning;
    • @skapaid 看看^_^
    • 我认为 setTextWithSpan(text,"宝宝名字的意思是 nam","baby",new android.text.style.StyleSpan(android.graphics.Typeface.BOLD));一次处理所有事情不是吗?
    • 不,对于单个文本而言,它不是静态的。它只是示例,您可以在字符串中包含 n 个粗体。在我的示例中,它需要婴儿字符串作为需要的字符串粗体
    • 字符串 common_name;字符串 common_name_meaning;是从数据库中提取的,而不是我的 .xml 字符串中的静态资源 我可以直接访问的唯一文本是“意思”这个词,但是我想设置 common_name +“意思”+ common_name_meaning 的样式,每个人都需要有不同的而不是相同的风格适用于它...对不起,如果我的问题令人困惑
    【解决方案5】:

    只需在 HTML 中构建您的字符串并设置它:

    String sourceString = "<b>" + id + "</b> " + name; 
    mytextview.setText(Html.fromHtml(sourceString));
    

    【讨论】:

    • 当字符串已经存在时,我如何构建字符串???这对我来说毫无意义.... String common_name;字符串 common_name_meaning;长 common_name_rowid; CharSequence mybreak = "\n"; CharSequence text = "意思"; Char序列描述;他们都已经在那里了我如何建立一个字符串并设置它?这对我来说毫无意义,因为我不能去 String common_name = "" + common_name + " ";
    • 我只是要放弃这个我还在学习和自学的人,这会在我弄清楚之前杀死我......我确定其中一个答案是正确的,所以我会留下这个问题为其他人提供帮助,以防他们可以使用它,并感谢您尝试帮助我!...不幸的是,在我理解这一点之前,我还有很多阅读要做
    【解决方案6】:

    尝试做

    tv.setText(Html.fromHtml(description));
    

    在描述字符串中使用&lt;b&gt;&lt;/b&gt; 标签。

    【讨论】:

    • 如果您的description 格式正确,' 已包裹,我相信这是一个正确的答案
    • 它抛出 2 个错误 ... 'fromHtml(java.lang.String)' 已被弃用并且 'android.text.Html' 中的 'fromHtml(java.lang.String)' 不能应用于'(java.lang.CharSequence)'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 2016-01-01
    • 2021-06-06
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    相关资源
    最近更新 更多