简体   繁体   中英

How to change TextView font using a Spinner android

So I'm trying to achieve the following: I want to have a spinner which has a list of fonts contained in it. And then the user can select one of these fonts and it will then update my TextView. I'm really new to android development so am struggling a bit. Any ideas?

On the spinner's setOnItemSelectedListener, add

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
                 Typeface custom_font = Typeface.createFromAsset(getAssets(),"fonts/fontname");
                 textview.setTypeface(custom_font);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

I suppose that the fonts are in your asset/fonts folder and the fontname might be retrieved from the spinner adapter.

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