2

我有自定义字体示例和一个小型列表视图示例应用程序。但我无法加入他们!

 /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv = (TextView)findViewById(R.id.tv);
    Typeface cFont = Typeface.createFromAsset(getAssets(), "fonts/jcc.ttf");
    tv.setTypeface(cFont);




<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
android:textSize="18sp"
android:id="@+id/tv"
/>

    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_list);

    String asim02 = System.getProperty("line.separator");

    String products[] = {
            "Apple" + asim02 +"Definition1", 
            "Orange" + asim02 +"Definition2",
            "Banana"+ asim02 +"Definition3", 
            "Onion"+ asim02 +"Definition4",  };

    lv = (ListView) findViewById(R.id.list_view);

    // Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.p_list,   products);
    lv.setAdapter(adapter);



<TextView

        android:textColor="?android:textColorPrimary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:id="@+id/p_list"
        />

请帮我自定义适配器以使用自定义字体

ProductList 应用程序的图像https://dl.dropbox.com/u/15065300/ProductList.png

这是两个示例应用程序http://www.mediafire.com/?scb3hjplb15yly5

4

4 回答 4

7

我通过创建一个 CustomTextView.class 来做到这一点

这将是我的list_item_layout.xml

<LinearLayout
    [...]

    android:orientation="vertical">
        <com.example.CustomTextView
             android:layout_width="fill_parent"
             [...]
             android:id="@+id/myCustomTextView"/>

</LinearLayout>

注意:你必须指向你的 CustomTextView.class 否则你会得到一个异常

这是 CustomTextView.class

public class CustomTextView extends TextView {

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomTextView(Context context) {
        super(context);
        init();
    }

    public void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/yourfont.ttf");
        setTypeface(tf);
    }

}

您可以将此应用于EditText,TextViewButtons

于 2012-12-02T03:46:15.130 回答
4

根据@FoamyGuy 的回复,我查看了您的 mediafire 文件回复,但您并没有完全修改arrayAdapter课程。我没有这些文件共享站点帐户之一,所以应该这样做:

在您的ProductList活动中,将您添加到列表视图代码的项目替换为:

    // Adding items to listview
    Typeface cFont = Typeface.createFromAsset(getAssets(), "fonts/jcc.ttf");
    adapter = new CustomArrayAdapter(this, R.layout.list_item, R.id.p_list, products, cFont);

然后是自定义类,您还必须插入:

public class CustomArrayAdapter extends ArrayAdapter <String> {

    private Typeface tf;
    private LayoutInflater inflater;
    private int resource;
    private int textViewResourceId;
    private String[] objects;

    public CustomArrayAdapter(Context context, int resource, int textViewResourceId,
            String[] objects, Typeface tf) {
        super(context, resource, textViewResourceId, objects);
        this.tf = tf;
        this.resource = resource;
        this.textViewResourceId = textViewResourceId;
        this.objects = objects;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

        if (convertView == null) {
            convertView = inflater.inflate(resource, parent, false);    
        }

        TextView text = (TextView) convertView.findViewById(textViewResourceId);
        text.setTypeface(tf);
        text.setText(objects[position]);
        return convertView;
    }
}

如果您需要查看完整的源文件夹,以免您犯同样的错误,请将您的电子邮件地址发送给我,我会发送给您。

于 2012-12-02T06:00:24.840 回答
1

一种方法是扩展 ArrayAdapter 让它为您设置字体。

下面是一个例子:

public class FontArrayAdapter extends ArrayAdapter<String>{
    private Typeface mFont;
    FontArrayAdapter(Context context, int textViewResourceId, Typeface font){
        mFont = font;
        super(context, textViewResourceId);
    }

    @Override
    public View getView(int pos, View convertView, ViewGoup parent){
        TextView tv;
        tv = (TextView) convertView;
        if(null == tv) {
            tv = new TextView(parent.getContext());
            tv.setTypeface(mFont);
        }

        tv.setText(getItem(pos));
    }
}
于 2012-12-02T03:46:51.393 回答
1

在你的 ArrayAdapter 中试试这个。将上下文添加到您的 getAssets() --> context.getAssets() 将字体文件存储在 assets 文件夹中。

public class ArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final ArrayList<String> values2;
    Typeface font;


public ArrayAdapter(Context context, ArrayList<String> values,ArrayList<String> values2) {
        super(context, R.layout.listview, values);
        this.context = context;
        this.values2 = values2;
}


@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.main, parent, false);
    font = Typeface.createFromAsset(context.getAssets(), "arial.ttf");  

    TextView textView = (TextView) rowView.findViewById(R.id.label2);
    textView.setTypeface(font);
}
return rowView;
}
}
于 2012-12-24T04:49:21.677 回答