How to set the font style to bold, italic and underlined in an Android TextView?

前端 未结 11 624
借酒劲吻你
借酒劲吻你 2020-12-02 04:04

I want to make a TextView\'s content bold, italic and underlined. I tried the following code and it works, but doesn\'t underline.



        
11条回答
  •  天涯浪人
    2020-12-02 04:25

    For bold and italic whatever you are doing is correct for underscore use following code

    HelloAndroid.java

     package com.example.helloandroid;
    
     import android.app.Activity;
     import android.os.Bundle;
     import android.text.SpannableString;
     import android.text.style.UnderlineSpan;
    import android.widget.TextView;
    
    public class HelloAndroid extends Activity {
    TextView textview;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textview = (TextView)findViewById(R.id.textview);
        SpannableString content = new SpannableString(getText(R.string.hello));
        content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
        textview.setText(content);
    }
    }
    

    main.xml

    
    
    

    string.xml

    
     
      Hello World, HelloAndroid!
      Hello, Android
    
    

提交回复
热议问题