Android TextView 设置字体大小的方法
废话不多说了,直接给大家贴代码了,具体代码如下所示:
packagecom.example.yanlei.yl4; importandroid.graphics.Color; importandroid.os.Bundle; importandroid.support.v7.app.AppCompatActivity; importandroid.text.Spannable; importandroid.text.style.AbsoluteSizeSpan; importandroid.text.style.BackgroundColorSpan; importandroid.text.style.StyleSpan; importandroid.view.Menu; importandroid.view.MenuItem; importandroid.view.View; importandroid.widget.Button; importandroid.widget.EditText; importandroid.widget.TextView; publicclassMainActivityextendsAppCompatActivity{ TextViewedit; ButtonsendBu; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit=(TextView)findViewById(R.id.textView); sendBu=(Button)findViewById(R.id.button); sendBu.setOnClickListener(newButtonClickListener()); } privateclassButtonClickListenerimplementsView.OnClickListener{ publicvoidonClick(Viewv){ //System.exit(0); edit.setText("闫磊我爱你"); edit.setTextColor(Color.BLUE); edit.setText("这是我的第一个TextView,嘿嘿",TextView.BufferType.EDITABLE); /** *要设置文本的背景色, *必须将文本设置成BufferType.SPANNABLE,BufferType.EDITABLE */ Spannablesp=(Spannable)edit.getText(); //设置红色背景 sp.setSpan(newBackgroundColorSpan(Color.RED),3,8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); sp.setSpan(newStyleSpan(android.graphics.Typeface.BOLD_ITALIC), 0,7,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//设置斜体 sp.setSpan(newAbsoluteSizeSpan(80),0,2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//设置字体大小 edit.setText(sp); //edit.setHeight(60); //android.os.Process.killProcess(android.os.Process.myPid()); } } @Override publicbooleanonCreateOptionsMenu(Menumenu){ //Inflatethemenu;thisaddsitemstotheactionbarifitispresent. getMenuInflater().inflate(R.menu.menu_main,menu); returntrue; } @Override publicbooleanonOptionsItemSelected(MenuItemitem){ //Handleactionbaritemclickshere.Theactionbarwill //automaticallyhandleclicksontheHome/Upbutton,solong //asyouspecifyaparentactivityinAndroidManifest.xml. intid=item.getItemId(); //noinspectionSimplifiableIfStatement if(id==R.id.action_settings){ returntrue; } returnsuper.onOptionsItemSelected(item); } }
以上代码内容是小编给大家分享的AndroidTextView设置字体大小的方法,希望对大家有所帮助。