【发布时间】: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;
}
}
【问题讨论】:
-
每个示例都令人困惑,因为它们是在单个字符串上或不使用数据库,谁能给我一个像我这样的代码示例?
-
@skapaid 字符串始终是一个字符串,不管它来自哪里,你可以说 String myName = "skapaid";并将 myName 应用到您要放置字符串的位置,或者您可以简单地将“skapaid”放在要放置字符串的位置。
标签: android android-layout textview