Android TextView sets the font size and androidtextview
Package com. example. yanlei. yl4;
Import android. graphics. Color;
Import android. OS. Bundle;
Import android. support. v7.app. AppCompatActivity;
Import android. text. Spannable;
Import android. text. style. AbsoluteSizeSpan;
Import android. text. style. BackgroundColorSpan;
Import android. text. style. StyleSpan;
Import android. view. Menu;
Import android. view. MenuItem;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. TextView;
Public class MainActivity extends AppCompatActivity {
TextView edit;
Button sendBu;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Edit = (TextView) findViewById (R. id. textView );
SendBu = (Button) findViewById (R. id. button );
SendBu. setOnClickListener (new ButtonClickListener ());
}
Private class ButtonClickListener implements View. OnClickListener {
Public void onClick (View v ){
// System. exit (0 );
Edit. setText ("Lei I Love You ");
Edit. setTextColor (Color. BLUE );
Edit. setText ("this is my first TextView, ", TextView. BufferType. EDITABLE );
/**
* Set the background color of the text,
* The text must be set to BufferType. SPANNABLE and BufferType. EDITABLE.
*/
Spannable sp = (Spannable) edit. getText ();
// Set the red background
Sp. setSpan (new BackgroundColorSpan (Color. RED), 3, 8,
Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
Sp. setSpan (new StyleSpan (android. graphics. Typeface. BOLD_ITALIC ),
0, 7, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); // set Italic
Sp. setSpan (new AbsoluteSizeSpan (80), 0, 2, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); // set the font size
Edit. setText (sp );
// Edit. setHeight (60 );
// Android. OS. Process. killProcess (android. OS. Process. myPid ());
}
}
@ 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 ){
// Handle action bar item clicks here. The action bar will
// Automatically handle clicks on the Home/Up button, so long
// As you specify a parent activity in AndroidManifest. xml.
Int id = item. getItemId ();
// Noinspection SimplifiableIfStatement
If (id = R. id. action_settings ){
Return true;
}
Return super. onOptionsItemSelected (item );
}
}