Android TextView sets the font size and androidtextview

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.