ListView Android中的自定义字体

时间:2015-11-17 09:08:20

标签: android listview android-listview custom-font

我在我的应用程序中使用ListView有一个自定义适配器,在getView方法中我使用TypeFace设置我的自定义字体。但我无法看到这些自定义字体,并且我的应用程序中也没有错误。 这是listview

的java文件
   public class Act_Category_ListView extends Activity {

    ListView act_list;
    //TextView act_tv;

    ArrayAdapter<String> listAdapter;
    String[] values = new String[]{
        "સેવકનુ ખોટુ", "do", "teen", "4", "5", "6", "6", "7", "8", "9", "10"
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      setContentView(R.layout.act_category_listview);
      act_list = (ListView) findViewById(R.id.act_category_listview);
      //  act_tv = (TextView)findViewById(R.id.tv_act_category);

        // TextView textView = (TextView) rowView.findViewById(R.id.tv_act_category);




        listAdapter = new CustomAdapter_ActCategory(this,R.layout.act_category_listview_textview,values);



        act_list.setAdapter(listAdapter);



        act_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int itemPosition = position;
                String itemValue = (String) act_list.getItemAtPosition(position);
                switch (itemPosition) {
                    case 0:
                        Toast.makeText(getApplicationContext(),
                                "Position :" + itemPosition + "  ListItem : " + itemValue, Toast.LENGTH_LONG)
                                .show();
                        Intent i1 = new Intent(Act_Category_ListView.this, Main_Activity.class);
                        startActivity(i1);
                        break;

                    case 1:
                        Toast.makeText(getApplicationContext(),
                                "Position :" + itemPosition + "  ListItem : " + itemValue, Toast.LENGTH_LONG)
                                .show();
                        Intent i2 = new Intent(Act_Category_ListView.this, Button2_Class.class);
                        startActivity(i2);
                        break;

                }
            }
        });

    }

}

CustomAdapter文件

    public class CustomAdapter_ActCategory extends ArrayAdapter<String> {

    private final Context context;
    int layoutResourceId;
    String[] values = new String[]{};
    TextView textView;
    Typeface tf;



    public CustomAdapter_ActCategory(Context context,int layoutResourceId,String[] values) {
        super(context,layoutResourceId,values);
        this.layoutResourceId=layoutResourceId;
        this.context=context;
        this.values=values;
       // tf = Typeface.createFromAsset(context.getAssets(),FONT);
       // act_tv = new TextView(context);
      //  act_tv.findViewById(R.id.tv_act_category);
    //    act_tv.setTypeface(tf); // set typeface here


    }

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

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.act_category_listview_textview, parent, false);
        textView = (TextView) rowView.findViewById(R.id.tv_act_category);
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),"fonts/NotoSansGujarati-Bold.ttf");
        textView.setTypeface(tf);
        textView.setTextColor(Color.DKGRAY);
        return rowView;
    }
}

ListView.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/act_category_listview"
    >

</ListView>

ListView的TextView.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_act_category"
android:layout_gravity="center_horizontal"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textColor="@color/dark_gray"
xmlns:android="http://schemas.android.com/apk/res/android" />

1 个答案:

答案 0 :(得分:0)

字体可能写得不好所以请尝试重新编码。尝试使用此免费在线工具http://www.freefontconverter.com/

上传您的字体文件并对其进行转换&#34;即使您将文件转换为相同的格式,该文件仍会被重新编码&#34;