Question

I am trying to implement fragment using android 4.0. I have added the three items in list fragment in my DummyContent.java file

static {
    // Add 3 sample items.
    addItem(new DummyItem("1", "Videos"));
    addItem(new DummyItem("2", "Images"));
    addItem(new DummyItem("3", "Story"));
}

These Videos,Images,Story are appearing on left side of fragment on click of each it item shows details information on detail fragment in right hand side.

I want to change the font of these list views but problem is list fragment uses systems textview as below

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO: replace with a real list adapter.
    setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1, DummyContent.ITEMS));
}

Which is not allowing me to apply Typeface to android.R.id.text1

Please help me

Était-ce utile?

La solution

One way to is to extend ArrayAdapter and override the getView method to get a hold of the TextView:

public class MyArrayAdapter<T> extends ArrayAdapter<T> {

    public MyArrayAdapter(Context context, List<T> items) {

        super(context, android.R.layout.simple_list_item_activated_1, android.R.id.text1, items);
    }

    public MyArrayAdapter(Context context, T[] items) {

        super(context, android.R.layout.simple_list_item_activated_1, android.R.id.text1, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = super.getView(position, convertView, parent);
        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        textView.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "font/segoeuil.tff"));

        return view;
    }
}

You would then set your adapter like so:

setListAdapter(new MyArrayAdapter<DummyContent.DummyItem>(getActivity(), DummyContent.ITEMS);

Autres conseils

You can use your own custom layout instead of predefined layouts Create an xml file like this

<?xml version="1.0" encoding="utf-8"?>
say your layout name be custom.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/cust_view"  
    android:layout_width="match_parent" 
    android:textSize="12dp"
    android:padding="6dp"
    android:layout_height="wrap_content" /> 

you can give your own font to this layout. and than set the adapter like this

setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
            R.layout.custom,
            android.R.id.text1, DummyContent.ITEMS));

I hope this will help you

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top