如何设置listview的字体(字体)?

时间:2014-02-23 22:10:18

标签: android listview android-listview typeface

我正在尝试将字体设置为我的幻灯片菜单列表视图,但我不知道该怎么做。我试试这个,但它不起作用:

ListView lvLeft = (ListView) leftView.findViewById(R.id.lvLeft);
lvLeft.settypeface(myface);

这是我的xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_left"
    android:layout_width="180dp"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/grey21"
        android:padding="10dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/optins"
            android:textColor="@color/gold"
            android:textSize="17sp" />
    </LinearLayout>

    <com.carlos.myslidingmenu.view.COFixListViewBugLinearLayout
        android:id="@+id/mylaout"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ListView
            android:id="@+id/lvLeft"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </ListView>
    </com.carlos.myslidingmenu.view.COFixListViewBugLinearLayout>

</LinearLayout>

这是我主要活动的一部分:

  

ListView lvLeft =(ListView)leftView.findViewById(R.id.lvLeft);

          //lvLeft.set
          ArrayAdapter<String> adapter1;
          adapter1 = new ArrayAdapter<String>(this, R.layout.item, R.id.tv_item, title);
          lvLeft.setAdapter(adapter1);
          lvLeft.setOnItemClickListener(new OnItemClickListener() {

              @SuppressWarnings("deprecation")
              @Override
              public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                  if(arg2==0){
                      coSlidingMenu.showViewState(COSlidingState.SHOWCENTER);
                      showDialog(dialog_num);
                  } }

我的列表项名称是item.xml。我在网上搜索过但没找到合适的答案。任何解决方案?

2 个答案:

答案 0 :(得分:0)

final Typeface mFont = Typeface.createFromAsset(getAssets(), "myfont.otf"); 
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewGroup view = (ViewGroup) super.getView(position, convertView, parent);
            if(convertView == null) MyActivity.setAppFont(view, mFont);
            return view;
        }
    };

答案 1 :(得分:0)

public class myadpter extends BaseAdapter {
    private ArrayList<String> listCountry;
    private Activity activity;

    public myadpter (Activity activity, ArrayList<String> listCountry)
    {
        super();
        this.listCountry = new ArrayList<String>();
        this.listCountry = listCountry;

        this.activity = activity;
        System.out.println("this is contry name " + this.listCountry);


    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        System.out.println("len " + listCountry.size());
        return listCountry.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return listCountry.get(position);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder {

        public TextView txtViewTitle;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if (convertView == null) {
            view = new ViewHolder();

            convertView = inflator.inflate(R.layout.custom_textview, null);

            view.txtViewTitle = (TextView) convertView
                    .findViewById(R.id.list_content);

            view.txtViewTitle.setTextColor(Color.BLACK);
            // Typeface typeface =
            // Typeface.createFromAsset(getAssets(),"fonts/DroidSerif.ttf");
            view.txtViewTitle.setTypeface(Typeface.createFromAsset(parent
                    .getContext().getAssets(), "fonts/DroidSerif.ttf"));

            // view.imgViewFlag.setBackgroundResource(R.drawable.view_default);
            convertView.setTag(view);

        }

        else {
            view = (ViewHolder) convertView.getTag();
        }
        System.gc();
        try {
            view.txtViewTitle.setText(listCountry.get(position));

        } catch (Exception e) {
            System.out.println("this is error " + e.getMessage());
        }

        return convertView;
    }

}

并使用此

mAdapter = new myadpter (this, listCountry);
        lv_diseases = (ListView) findViewById(R.id.lv_diseases);


        lv_diseases.setAdapter(mAdapter);

listCountry = new ArrayList<String>();
listCountry.addAll(list);