0

您好我正在尝试为 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
} 
4

1 回答 1

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
} 
于 2013-10-21T17:23:47.243 回答