如何在Android Java中加粗,改变字体大小,textview的颜色不同部分
我有一个Android应用程序,该应用程序列出了人们的婴儿名字.....它有一个 textview
,它可以连接不同的文本和数据字符串,然后显示它们……(宝宝的名字+手段" +名字的含义)
I have an android app that lists baby names for people ..... it has a textview
that connects different strings of text and data and then displays them...... (the baby name + "means" + the meaning of the name)
我正在尝试更改字体大小和颜色,并加粗文本的第一个字符串(婴儿名字),然后在其后添加水平线或分隔线或空格.
I am trying to change the font size and color and bold the first string of the text (the baby name) and then add a horizontal line or divider or spacer after it.
然后,我想分别设置第二个文本字符串(均值"文本)的字体大小和颜色的样式,并将其设置为粗体.
Then I would like to style the second text string (the "means" text) font size and color separately and make it bold as well.
然后我想分别设置第三个字符串的字体大小和颜色的样式,并使其也为粗体.
then i would like to style the third string font size and color separately and make it bold as well.
我一直在阅读有关 SpannableString
的信息,并尝试在最近3个小时内实现它,但是没有运气.如果有人可以提供帮助,将不胜感激!
I have been reading about SpannableString
and tried to implement it for the last 3 hours with no luck.If anyone could help with this it would be greatly appreciated!
这是我当前拥有的工作代码
Here is the current working code I have
我的 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">
MY 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标签不起作用....这只是为了显示我要在其中包装文本的内容以及我要在其中放置分隔符的位置)
and here is an example of what i am trying to do
(Note I know html tags don't work .... that is just to show what i am trying to wrap the text in and where i am trying to put the divider)
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;
}
}
尝试做
tv.setText(Html.fromHtml(description));
在描述字符串中带有< b>
和</b>
标签.
With the <b>
and </b>
tags in your description string.