如何在android java

时间:2017-02-07 02:44:43

标签: android android-layout textview

我有一个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">

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标签不起作用....这只是为了展示我想要包装文本的内容以及我试图放置分隔符的位置)

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

}

6 个答案:

答案 0 :(得分:2)

尝试

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

使用说明字符串中的<b></b>标记。

答案 1 :(得分:0)

简单

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

输出

enter image description here

有关属性的额外知识,请参阅How to set the font style to bold, italic and underlined in an Android TextView?

答案 2 :(得分:0)

只需在HTML中构建您的String并进行设置:

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return UITableViewAutomaticDimension

        }

答案 3 :(得分:0)

试试这个,

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

答案 4 :(得分:0)

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

For Detail go through this link

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

答案 5 :(得分:-1)

我想通了.......

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

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

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