有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android textview具有两种不同大小的文本

我试图在一个文本视图中设置不同文本大小的html文本,但什么都没有

我试着这样:

textViewNextTime.setText(Html.fromHtml("<h2>4</h2><p>38</p>"));

我也尝试过使用和标签,但没有成功。有谁能帮我创建这样的文本视图(当然没有圆圈):

enter image description here

编辑:在示例中4的大小应该类似于140sp,而:38的大小应该类似于70sp


共 (2) 个答案

  1. # 1 楼答案

    如果您不需要使用html,可以尝试以下方法:

    String s= "4:38";
    SpannableString ss1=  new SpannableString(s);
    ss1.setSpan(new RelativeSizeSpan(2f), 0, 1, 0); // set size
    TextView tv= (TextView) findViewById(R.id.textview);
    tv.setText(ss1); 
    
  2. # 2 楼答案

    试试这个: 主要活动。java

    public class MainActivity extends Activity {
    
        private final String htmlText = "<body><h1>Heading Text</h1><p>This tutorial " +
                "explains how to display " +
                "<strong>HTML </strong>text in android text view.&nbsp;</p>" +
                "<img src=\"hughjackman.jpg\">" +
                "<blockquote>Example from <a href=\"www.javatechig.com\">" +
                "Javatechig.com<a></blockquote></body>";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            TextView htmlTextView = (TextView)findViewById(R.id.html_text);
            htmlTextView.setText(Html.fromHtml(htmlText, new ImageGetter(), null));
    
        }
    
        private class ImageGetter implements Html.ImageGetter {
    
            public Drawable getDrawable(String source) {
                int id;
                if (source.equals("hughjackman.jpg")) {
                    id = R.drawable.abc_ic_ab_back_mtrl_am_alpha;
                }
                else {
                    return null;
                }
    
                Drawable d = getResources().getDrawable(id);
                d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
                return d;
            }
        };
    
    }
    

    主要活动。xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/title_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Your HTML text Below"/>
    
        <TextView
            android:id="@+id/html_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
    </LinearLayout>