【发布时间】:2014-09-07 00:52:15
【问题描述】:
我是 android 编程新手,我正在努力解决以下问题。 我正在使用自定义 ListView 来显示商店名称及其徽标。 我想将标准 textview 字体更改为自定义字体。
我用于普通文本视图的方法不起作用。 你能帮帮我吗?
以下代码是我的 ListView 适配器。
package com.mallspecials.shoppingmallspecialsau;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ShowTheStoreAdapter extends BaseAdapter {
// Declare Variables
Context context;
String[] thestore;
int[] logo;
LayoutInflater inflater;
public ShowTheStoreAdapter(Context context, String[] thestore, int[] logo) {
this.context = context;
this.thestore = thestore;
this.logo = logo;
}
private AssetManager getAssets() {
// TODO Auto-generated method stub
return null;
}
@Override
public int getCount() {
return thestore.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
//Set Snell Roundhand Font for the StoreName
Typeface font = Typeface.createFromAsset(getAssets(), "Snell Roundhand Bold Script.ttf");
// Declare Variables
TextView StoreName;
ImageView imglogo;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_stores, parent, false);
//Set the StoreName Text
StoreName = (TextView) itemView.findViewById(R.id.thestore);
StoreName.setTextColor(Color.RED);
StoreName.setShadowLayer(3, -2, -5,(Color.YELLOW));
StoreName.setTypeface(font);
StoreName.setText(thestore[position]);
// Locate the ImageView in listview_stores.xml
imglogo = (ImageView) itemView.findViewById(R.id.storelogo);
// Capture position and set to the ImageView
imglogo.setImageResource(logo[position]);
return itemView;
}
}
下面是我的适配器视图实现的代码。
list = (ListView) findViewById(R.id.listview);
adapter = new ShowTheStoreAdapter(this, thestore, storelogo);
list.setAdapter(adapter);
【问题讨论】:
-
“不工作”是什么意思?您是否收到可以分享的错误消息或可以描述的不良结果?
-
对不起,斯科特,当我点击上一个活动时,手机屏幕上显示错误消息“不幸的是,“我的应用名称”已停止。
-
如果我在不尝试设置自定义字体的情况下运行应用程序,它会完美运行。
标签: android listview android-listview