更改我的listView中的字体

时间:2014-08-18 09:14:34

标签: android listview

所以我的这个列表视图让我感到头疼。它完美无缺,但我似乎无法改变lV中文本的字体。

这是我的代码:

public class MyActivity3 extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my3);
    Button m = (Button) findViewById(R.id.button3);
    tv = (TextView) findViewById(R.id.textViewcat);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    tv.setTypeface(typeface);


    String listArray[] = new String[] { "India", "England", "Canada",
            "New zealand", "South Africa", "Pakistan", "West indies" };
    int icon[] = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher };

    ListView listView = (ListView) findViewById(R.id.listView);
    List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

    for (int i = 0; i <= listArray.length - 1; i++) {

        HashMap<String, String> hm = new HashMap<String, String>();
        hm.put("title", listArray[i]);
        hm.put("icon", Integer.toString(icon[i]));
        aList.add(hm);
    }

    String[] sfrm = { "title", "icon" };
    int[] sto = { R.id.title, R.id.list_image};

    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
            R.layout.row_layout, sfrm, sto);



    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                                int position, long id) {

            switch (position) {

                case 0:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 4:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 5:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 6:
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_SHORT).show();
                    break;

            }

        }
    });
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.animation8, R.anim.animation7);
    }
}

row_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >


<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dip"
    android:padding="3dip" >

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/ic_launcher" />
</LinearLayout>



<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Country name"
    android:textColor="#FFFFFF"
    android:textSize="30sp"
    android:typeface="sans" />

</RelativeLayout>

这就是我的代码,因为你可以看到有一个id为“title”的textview。如何更改标题字体?如果你根据我的代码回答问题,我真的很赞赏,因为我已经在SO上尝试了所有其他解决方案,但没有成功

4 个答案:

答案 0 :(得分:1)

以下是该问题的详细说明。

http://javatechig.com/android/using-external-fonts-in-android-view

基本上对你来说,因为它在列表视图中,我建议你创建自定义文本视图。将字体文件放在assets文件夹中非常重要。

说您尝试了所有其他来源但没有成功是错误的,因为上面的链接看起来像第二个谷歌搜索结果并且运行正常。

答案 1 :(得分:1)

嗨克里斯蒂亚诺,

我想我已经找到了改变字体的问题。

At your code:
Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
tv.setTypeface(typeface);

Change like below and try out:
Typeface typeface = Typeface.createFromAsset(getAssets(), "customfont.ttf");
tv.setTypeface(typeface);

请将ttf文件名改为任何小写字母的名称,字母之间没有空格,然后尝试一下就可以了。

答案 2 :(得分:0)

您必须创建自定义适配器,并在getView方法中将字体放入您的视图中:

public class ListAdapter extends ArrayAdapter<Item> {

public ListAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
}

public ListAdapter(Context context, int resource, List<Item> items) {
    super(context, resource, items);
}

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

    View v = convertView;

    if (v == null) {

        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.itemlistrow, null);

    }
    TextView tv = (TextView) v.findViewById(R.id.tv_name);
    tv.setTypeface(typeFace);
    ...
}

答案 3 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

public class MyActivity3 extends Activity {
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my3);
        Button m = (Button) findViewById(R.id.button3);
        tv = (TextView) findViewById(R.id.textViewcat);
        Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
        tv.setTypeface(typeface);


        String listArray[] = new String[] { "India", "England", "Canada",
                "New zealand", "South Africa", "Pakistan", "West indies" };
        int icon[] = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher };

        ListView listView = (ListView) findViewById(R.id.listView);
        ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

        for (int i = 0; i <= listArray.length - 1; i++) {

            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("title", listArray[i]);
            hm.put("icon", Integer.toString(icon[i]));
            aList.add(hm);
        }
//
//        String[] sfrm = { "title", "icon" };
//        int[] sto = { R.id.title, R.id.list_image};
//
//        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
//                R.layout.row_layout, sfrm, sto);


        listView.setAdapter(new CustomAdapter(this,aList));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                                    int position, long id) {

                switch (position) {

                    case 0:
                        Toast.makeText(getApplicationContext(), "",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Toast.makeText(getApplicationContext(), "",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(getApplicationContext(), "",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        Toast.makeText(getApplicationContext(), "",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 4:
                        Toast.makeText(getApplicationContext(), "",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 5:
                        Toast.makeText(getApplicationContext(), "",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case 6:
                        Toast.makeText(getApplicationContext(), "",
                                Toast.LENGTH_SHORT).show();
                        break;

                }

            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.animation8, R.anim.animation7);
    }

    class CustomAdapter extends BaseAdapter{
        private Context context;
        private ArrayList<HashMap<String,String>> data;
        public CustomAdapter(Context context,ArrayList<HashMap<String,String>> data){
            this.context=context;
            this.data=data;
        }
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView==null){
                viewHolder =new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.row_layout,null);
                viewHolder.list_image = (ImageView) convertView.findViewById(R.id.list_image);
                viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                Typeface typeface = Typeface.createFromAsset(getAssets(), "yourcustomfontname.ttf");
                viewHolder.title.setTypeface(typeface);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.title.setText(data.get(position).get("title"));
            viewHolder.list_image.setImageResource(Integer.parseInt(data.get(position).get("icon")));
            return null;
        }

        class ViewHolder{
            ImageView list_image;
            TextView title;
        }
    }
}