如何在android java中更改文本视图的粗体、字体大小和颜色

如何在android java中更改文本视图的粗体、字体大小和颜色,android,android-layout,textview,Android,Android Layout,Textview,我有一个android应用程序,可以为人们列出婴儿的名字。。。。。它有一个textview,连接不同的文本和数据字符串,然后显示它们。。。。。。(婴儿名字+“表示”+名字的含义) 我试图改变字体大小和颜色,加粗文本的第一个字符串(婴儿的名字),然后在后面添加水平线、分隔符或间隔符 然后我想分别设置第二个文本字符串(“means”文本)的字体大小和颜色,并将其加粗 然后我想分别设置第三个字符串的字体大小和颜色,并将其加粗 我一直在阅读关于SpannableString的文章,在过去的3个小时里,我

我有一个android应用程序,可以为人们列出婴儿的名字。。。。。它有一个
textview
,连接不同的文本和数据字符串,然后显示它们。。。。。。(婴儿名字+“表示”+名字的含义)

我试图改变字体大小和颜色,加粗文本的第一个字符串(婴儿的名字),然后在后面添加水平线、分隔符或间隔符

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

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

我一直在阅读关于
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">
下面是我正在尝试做的一个例子
注意我知道html标记不起作用……这只是为了显示我试图包装文本的内容以及我试图放置分隔符的位置)

package mypackage.android;
导入android.content.Intent;
导入android.net.Uri;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.widget.TextView;
导入android.widget.Toast;
导入mypackage.android.database.CommonNamesAdapter;
公共类CommonNameDescription扩展了AppCompative活动{
字符串common_name;<---需要更改字体大小和颜色并加粗
字符串common_name_means;<---需要更改字体大小和颜色,并将粗体与上面的字符串分开
长公共_名称_rowid;
CharSequence mybreak=“\n”;
CharSequence text=“Means”<---需要更改字体大小和颜色,并将粗体与上面的其他两个字符串分开
字符序列描述;
公共静态文本视图电视;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.common_name_description);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
Bundle extras=getIntent().getExtras();
common\u name\u rowid=extras.getLong(CommonNamesAdapter.common\u name\u rowid);
common_name=extras.getString(commonNameAdapter.common_name);
common_name_meaning=extras.getString(commonNameAdapter.common_name_meaning).toString();
description=公共名称+水平线或分割线+文本+mybreak+公共名称\u含义;
tv.setText(说明);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
布尔bRet=false;//处理菜单选择时设置true
开关(item.getItemId()){
案例R.id.action\u settings\u get\u pro:
意向意向=新意向(意向.行动\视图);
setData(Uri.parse(getString(R.string.pro_version_url));
星触觉(意向);
bRet=真;
打破
案例R.id.action\u settings\u get\u pro2:
意向意向2=新意向(意向.行动\视图);
intent2.setData(Uri.parse(getString(R.string.pro_version_url));
星触觉(intent2);
bRet=真;
打破
案例R.id.action\u settings\u app\u help:
Toast.makeText(this,this.getString(R.string.action\u settings\u app\u help\u text),Toast.LENGTH\u SHORT.show();
bRet=真;
打破
案例R.id.action\u设置\u关于\u应用程序:
Toast.makeText(this,this.getString(R.string.action\u settings\u about\u text),Toast.LENGTH\u SHORT.show();
bRet=真;
打破
案例R.id.action\u settings\u rate\u应用程序:
意向意向3=新意向(意向.行动\视图);
intent3.setData(Uri.parse(getString(R.string.rate\u this\u app\u url));
起始触觉(intent3);
bRet=真;
打破
案例R.id.行动\设置\隐私\政策:
Intent intentprivacy=新意图(Intent.ACTION\u视图);
intentprivacy.setData(Uri.parse(getString(R.string.privacy_policy_url));
主动性(意向性);
bRet=真;
打破
案例R.id.action\u设置\u所有\u我们的\u应用程序:
意向意向4=新意向(意向.行动\视图);
intent4.setData(Uri.parse(getString(R.string.all\u-our\u-apps\u-url));
起始触觉(强度4);
bRet=真;
打破
违约:
bRet=超级选项已选择项(项);
}
返回布雷特;
}
}
试试看

tv.setText(Html.fromHtml(description));
使用描述字符串中的
标记。

简单

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

编辑

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

你有三个主要的改动要做。我的答案是在同一个字符串中演示粗体、斜体和不同的字体大小。应用相同的概念并根据需要进行调整

  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));
输出


有关属性的更多知识,请参阅。

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

String sourceString = "<b>" + id + "</b> " + name; 
mytextview.setText(Html.fromHtml(sourceString));
String sourceString=”“+id+“”+name;
mytextview.setText(Html.fromHtml(sourceString));
试试这个

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));
<代码
String sourceString = "<b>" + id + "</b> " + name; 
mytextview.setText(Html.fromHtml(sourceString));
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));
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);