How to apply FontSize dynamically and programmatically without using XML in Android? [duplicate]

我的梦境 提交于 2020-01-28 10:23:10

问题


Possible Duplicate:
How to change the Font Size in a whole Application programmatically, Android?

This is my Code : I have created Spinner with a list of FontSize options. If I click FontSize "26" then it should be able to change in that specific FontSize. Below I have an EditBox. So if I clicked Fontsize as 40 and in a Italic style than usual Bold. So I should be able to type inside EditBox with this selected "Font" : FontSize "40" and Italic style.

How could I do this programmatically in Android?

font=new Spinner(con);
option= new String[] {"Select Font Size","8","10","12","14","16","18","20",
                      "22","24","26","28","30","32","34","36","38","40","50"};
ArrayAdapter<String> adapter= new ArrayAdapter<String>(con,android.R.layout.simple_spinner_dropdown_item,option);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

font.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id) {
        option[1]="8";
       selectedItem= option[position];
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    }
});

回答1:


to change

textSize use editText.setTextSize(20)

font and style use editText.setTypeface(yourTypeFace, Typeface.BOLD)

UPDATE

public class MyEditText extends EditText{

public MyEditText(Context context) {
    super(context);
    init();
}

public MyEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}
public MyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}
void init() {
   this.setTextSize(20);
   this.setTypeface(yourTypeFace, Typeface.BOLD);
}

// method to change font settings
void setFont(TypeFace tf){
   this.setTypeFace(tf);
}
//add whatever method you want
}

and then instead of using EditText you use this class, and don't forget in your XML to use

<yourpackage.MyEditText
     android:layout_height=".."
     android:layout_width=".."
     ... />



回答2:


The probable solution would be you create a base class which extends TextView, and use this text view class as edit text. Hope you are asking for size in first screen. In any case, u set the text size in the base class. This will solve your problem.

like u create this class in package com.example and class name is BaseTextView, then in xml file instead of you will write

Hope this helps.




回答3:


First of all get the id of the EditBox. Then selected item's position. according to that you can make a formula like this:

public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id) {
    int p=(8+(pos*2));

    editText.setTextSize(p);
}


来源:https://stackoverflow.com/questions/12706166/how-to-apply-fontsize-dynamically-and-programmatically-without-using-xml-in-andr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!