Android - set TextView TextStyle programmatically?

前端 未结 11 632
南旧
南旧 2020-12-02 05:04

Is there a way to set the textStyle attribute of a TextView programmatically? There doesn\'t appear to be a setTextStyle() method.

11条回答
  •  时光说笑
    2020-12-02 05:53

    So many way to achieve this task some are below:-

    1.

    String text_view_str = "Bolded text, italic text, even underlined!";
    TextView tv = (TextView)findViewById(R.id.ur_text_view_id);
    tv.setText(Html.fromHtml(text_view_str));
    

    2.

    tv.setTypeface(null, Typeface.BOLD);
    tv.setTypeface(null, Typeface.ITALIC);
    tv.setTypeface(null, Typeface.BOLD_ITALIC);
    tv.setTypeface(null, Typeface.NORMAL);
    

    3.

    SpannableString spannablecontent=new SpannableString(o.content.toString());
    spannablecontent.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 
                             0,spannablecontent.length(), 0);
    // set Text here
    tt.setText(spannablecontent);
    

    4.

    
    
    
        
    
        
    
    
    
     tv.setTextAppearance(getApplicationContext(), R.style.boldText);
    

    or if u want through xml

    android:textStyle="normal"
    android:textStyle="normal|bold"
    android:textStyle="normal|italic"
    android:textStyle="bold"
    android:textStyle="bold|italic"
    

提交回复
热议问题