listview textview中的自定义字体 - 无法对getAssets进行静态引用

时间:2013-10-21 17:18:58

标签: android listview

您好我正在尝试为TextView设置自定义字体,我收到错误,因为无法从contextWrapper中对非静态方法getAssets()进行静态引用。如何设置自定义字体? 。我的代码如下。有没有办法在XML布局文件本身中设置字体。它存在于资产中。

public class MainActivity extends Activity {

    private static class EfficientAdapter extends BaseAdapter {
        Typeface tfTab = Typeface.createFromAsset(getAssets(),"fonts/tabkovai.ttf");
        private LayoutInflater mInflater;

        public EfficientAdapter(Context context) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);

            // Icons bound to the rows.
        }

        /**
         * The number of items in the list is determined by the number of speeches
         * in our array.
         *
         * @see android.widget.ListAdapter#getCount()
         */
        public int getCount() {
            return DATA.length;
        }

        /**
         * Since the data comes from an array, just returning the index is
         * sufficent to get at the data. If we were using a more complex data
         * structure, we would return whatever object represents one row in the
         * list.
         *
         * @see android.widget.ListAdapter#getItem(int)
         */
        public Object getItem(int position) {
            return position;
        }

        /**
         * Use the array index as a unique id.
         *
         * @see android.widget.ListAdapter#getItemId(int)
         */
        public long getItemId(int position) {
            return position;
        }

        /**
         * Make a view to hold each row.
         *
         * @see android.widget.ListAdapter#getView(int, android.view.View,
         *      android.view.ViewGroup)
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.rowlayout, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);
                convertView.setTag(holder);

            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder.
            holder.text.setText(DATA[position]);
            return convertView;
        }

        static class ViewHolder {
            TextView text;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView lv = (ListView) findViewById(R.id.list);
        lv.setFocusable(true);
        lv.setFastScrollEnabled(true);
        lv.setAdapter(new EfficientAdapter(this));
        lv.setOnItemClickListener(new OnItemClickListener()
        {
            @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
            { 
                Toast.makeText(getApplicationContext(), "Hello : " + position, Toast.LENGTH_SHORT).show();
            }
        });

    }

    private static final String[] DATA = {"Üô¢ ð£î¢î¤ý£ - «î£ø¢Áõ£ò¢"};// Lot of strings
} 

1 个答案:

答案 0 :(得分:0)

您的EfficientAdapter课程是静态的。这意味着没有活动的实例可用,因此您无法直接调用其getAssets()方法。使EfficientAdapter非静态以获取封闭类的方法。

...
private class EfficientAdapter extends BaseAdapter {
...

编辑:正如Raghunandan指出这是不好的做法。这是更好的代码。我们甚至不需要对活动的引用,因为我们仅将其用于资产的Context

public class MainActivity extends Activity {

    private static class EfficientAdapter extends BaseAdapter {

        Typeface tfTab; // We can't initialize the font yet as we have no Context

        private LayoutInflater mInflater;

        public EfficientAdapter(Context context) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);

            // Now get the font from the assets as we have a Context to do it with
            tfTab = Typeface.createFromAsset(context.getAssets(),"fonts/tabkovai.ttf");

            // Icons bound to the rows.
        }

        /**
         * The number of items in the list is determined by the number of speeches
         * in our array.
         *
         * @see android.widget.ListAdapter#getCount()
         */
        public int getCount() {
            return DATA.length;
        }

        /**
         * Since the data comes from an array, just returning the index is
         * sufficent to get at the data. If we were using a more complex data
         * structure, we would return whatever object represents one row in the
         * list.
         *
         * @see android.widget.ListAdapter#getItem(int)
         */
        public Object getItem(int position) {
            return position;
        }

        /**
         * Use the array index as a unique id.
         *
         * @see android.widget.ListAdapter#getItemId(int)
         */
        public long getItemId(int position) {
            return position;
        }

        /**
         * Make a view to hold each row.
         *
         * @see android.widget.ListAdapter#getView(int, android.view.View,
         *      android.view.ViewGroup)
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.rowlayout, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);
                convertView.setTag(holder);

            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder.
            holder.text.setText(DATA[position]);
            return convertView;
        }

        static class ViewHolder {
            TextView text;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView lv = (ListView) findViewById(R.id.list);
        lv.setFocusable(true);
        lv.setFastScrollEnabled(true);
        lv.setAdapter(new EfficientAdapter(this));
        lv.setOnItemClickListener(new OnItemClickListener()
        {
            @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
            { 
                Toast.makeText(getApplicationContext(), "Hello : " + position, Toast.LENGTH_SHORT).show();
            }
        });

    }

    private static final String[] DATA = {"Üô¢ ð£î¢î¤ý£ - «î£ø¢Áõ£ò¢"};// Lot of strings
}