Ste*_*ven 5 fonts android listview android-listview
我已经找到了许多不同的方法来实现这一点,但我不确定什么是最适合我的方案.
这是我的listview的Java代码:
ListView lv;
lv = (ListView) findViewById(R.id.favList);
Run Code Online (Sandbox Code Playgroud)
这是列表的xml代码:
<ListView
android:id="@+id/favList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:listSelector="@android:color/transparent" >
</ListView>
Run Code Online (Sandbox Code Playgroud)
对于文本视图,我会添加:
final Typeface fontList = Typeface.createFromAsset(assets, "optima-extra-black.ttf");
lv.setTypeface(fontList);
Run Code Online (Sandbox Code Playgroud)
但这对listviews不起作用.在这种情况下如何更改字体?
我几乎就在那里......我需要访问我的资产,但我无法从我的自定义适配器中访问.我试过用final AssetManager assets = this.getAssets();但不会让我更进一步..
如何解决这个问题?
class Myadapter extends BaseAdapter {
LayoutInflater lif;
ImageView sideArrow;
TextView tv;
public Myadapter(Context ctx) {
lif = (LayoutInflater) ctx
.getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return favarets.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = lif.inflate(R.layout.inflate, null);
sideArrow = (ImageView) vi.findViewById(R.id.imageViewsidemark);
tv = (TextView) vi.findViewById(R.id.textFav);
tv.setText(favarets.get(position));
final AssetManager assets = this.getAssets();
final Typeface tvFont = Typeface.createFromAsset(assets, "OPTIMA.TTF");
tv.setTypeface(tvFont);
tv.setTextColor(Color.BLACK);
return vi;
Run Code Online (Sandbox Code Playgroud)
我找到了解决方案:D
public class Myadapter extends BaseAdapter {
AssetManager assetManager = getAssets();
LayoutInflater lif;
ImageView sideArrow;
TextView tv;
public Myadapter(Context ctx) {
lif = (LayoutInflater) ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return favarets.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = lif.inflate(R.layout.inflate, null);
sideArrow = (ImageView) vi.findViewById(R.id.imageViewsidemark);
tv = (TextView) vi.findViewById(R.id.textFav);
tv.setText(favarets.get(position));
final Typeface tvFont = Typeface.createFromAsset(assetManager, "OPTIMA.TTF");
tv.setTypeface(tvFont);
tv.setTextColor(Color.BLACK);
return vi;
}
}
Run Code Online (Sandbox Code Playgroud)
列表视图本身不负责绘制项目,它使用适配器来创建列表项.此适配器创建一个视图以在需要时显示列表项.要更改用于显示列表项的字体,您必须更改适配器以返回包含新字体的视图.这可以在Adapter.getView方法中完成.如果您当前正在使用标准的Adapter实现,则可能需要对其进行子类化(或完全替换它).
| 归档时间: |
|
| 查看次数: |
27831 次 |
| 最近记录: |