简体   繁体   中英

I can't set custom font in android view

I have a spinner to choose between some custom fonts. so I tried following codes to set this spinner. But there is an error says (cannot resolve symbol creatFromAsset). I don't know where I made a mistake!

try {
      font.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_spinner_item, fonts)); 
} catch (Exception ex){
      Toast.makeText(MainActivity.this,"setAdapters Error", Toast.LENGTH_SHORT).show();
}

font.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch(position) {
            case 0:
                Typeface typeface  = new  Typeface.createFromAsset(getAssets(),"assets/Xanadu.ttf");
                edt2.setTypeface(typeface);
        }
    }
});

TypeFace constructor is not public, so you can't use new .

Do instead:

Typeface typeface = Typeface.createFromAsset(getAssets(),"Xanadu.ttf");

getAssets本身指向资产的文件夹,您无需在文件地址中再次重复资产

Typeface typeface  = Typeface.createFromAsset(getAssets(),"Xanadu.ttf");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM