Android: Own Font in ListView

2019-08-04 05:47发布

问题:

I'm trying to have my own font in a listview using Java in Android OS 2.3.x

After reading the following links, i'm stuck:

http://developer.android.com/guide/topics/ui/themes.html http://stackoverflow.com/questions/4576441/custom-font-in-android-listview http://androidforums.com/android-lounge/143874-custom-typface-listview-row-layout.html

i can't post something usefull here.

My main problems are:

  • Why can't i change the Font for a listview, using setTypeface?
  • Why can't i define a Font for ALL texts in my application without putting it in every activity again?
  • Is there a documentation, which handles this problems? The Android SDK documentation is lacking a huge amount of details, like at which version otf Fonts are working.

I know that i have to learn many things and i'm ready as much books about this topics as i can. But after two days guessing and trying around, i have to ask for help.

Propably someone can push me in the right direction ;)

To give you a small idea, i tried to clean up some code:

File: res/layout/scores.xml (snippet from it)

<ListView
    android:id="@+id/android:list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00000000"
    android:cacheColorHint="#00000000"
    android:transcriptMode="disabled" >
</ListView>

File: src/notneededasinfo/ScoresActivity.java (snippet from it)

Resources myResources = getResources();

Typeface tf = Typeface.createFromAsset(myResources.getAssets(),"fonts/searstower.ttf");
TextView tv = (TextView) findViewById(android.R.id.list);
tv.setTypeface(tf);

Thx for help!

回答1:

Well... first of all android:textStyle and setTypeface(Typeface) are not a property and function from ListView, it is from TextView.

Try to use this tutorial: http://developer.android.com/resources/tutorials/views/hello-listview.html

Anyway, you have to define a .xml file that defines the style for the itens in the ListView. At the link I posted before you can see on topic 4 the handler onCreate the following line:

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));

So, you have to define the layout for the ListView item on the file list_item.xml used above as R.layout.list_item. On this file, list_item.xml, you define new TextViews or EditTexts or whatever and changes the fonts individualy for each View.

Hope it helps you...



回答2:

TextView tv = (TextView) findViewById(android.R.id.list);

You need to have a text view to set the font. You are missing the list row view.



回答3:

I have a answer for one of your questions

Why can't i define a Font for ALL texts in my application without putting it in every activity again?

This is a known problem but easy to solve. Just implement a custom activity class which extends Activity and overwrite the setContentView() method. In this Method you can get all your TextViews for your current view and set the Typface

See here: Add custom font for complete android application