Change font of list fragment on Detail fragment

风格不统一 提交于 2019-12-22 08:24:45

问题


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


回答1:


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);



回答2:


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



来源:https://stackoverflow.com/questions/14190648/change-font-of-list-fragment-on-detail-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!