如何更改EditText字体系列

时间:2014-12-08 13:50:15

标签: android android-edittext

我必须通过从下拉列表中选择选项来更改EditText的font-family,然后检查下面的代码 我的问题是如何通过代码>>更改font-family并让用户选择他的字体系列..

 public class Graduation extends ActionBarActivity {

ImageView imageView ;
 Spinner spinner1;
 int fontSizeFamily;
String[]f_items = { "Times New Roman ", "Arial", "sans-serif", "20", "24", "28" , "30" };
 EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.graduation);


    spinner1 = (Spinner)findViewById(R.id.spinner1);  // spinner1 exist in xml file
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
        this, android.R.layout.simple_spinner_item, f_items);
    spinner.setAdapter(adapter1);
    spinner.setOnItemSelectedListener(
              new AdapterView.OnItemSelectedListener() {
                  @Override
                  public void onItemSelected(AdapterView<?> arg0, View arg1,
                          int arg2, long arg3) {
                    int position = spinner1.getSelectedItemPosition();

                    try
                    {
                    if (position == 0 )
                        {// whats the code here ??? }
                    }
                     if (position == 1 )
                        {// whats the code here ??? }
                    }
                    catch (NumberFormatException e)
                    {
                        // whats the code here  ? 
                    }

                  }
                  @Override
                  public void onNothingSelected(AdapterView<?> arg0) {
                      // TODO Auto-generated method stub
                  }
              }
          );

所以我不知道在if-statment中放什么?

3 个答案:

答案 0 :(得分:1)

Typeface font = Typeface.createFromAsset(getAssets(),"yourFont.ttf");
et.setTypeFace(font);

答案 1 :(得分:0)

您可以更改任何视图的字体

yourView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));

答案 2 :(得分:0)

第1步:下载字体文件并将其(.ttf.otf文件)放入asset/font这样的内容(您需要在资源中创建字体文件夹) ):

enter image description here

第2步:在@Decoy所提及的if / else逻辑中添加代码:

Typeface f = Typeface.createFromAsset(getContext().getAssets(),"font/Name_of_Font_File.ttf");
et.setTypeFace(f);

使用getContext().getAssets()