问题描述
我找到了实现这个不同的方式加载,但我不知道什么是最适合我的情况。
这是我的Java code对于ListView:
的ListView LV;
LV =(ListView控件)findViewById(R.id.favList);
这是XML code的清单:
<的ListView
机器人:ID =@ + ID / favList
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginBottom =40dp
机器人:layout_marginLeft =30dp
机器人:layout_marginRight =30dp
机器人:layout_marginTop =20dp
机器人:背景=@机器人:彩色/透明
机器人:cacheColorHint =@机器人:彩色/透明
机器人:listSelector =@机器人:彩色/透明>
< / ListView控件>
有关文本视图我想补充:
最后的字样了FontList = Typeface.createFromAsset(资产,最优-额外black.ttf);
lv.setTypeface(了FontList);
但是,这并不为列表视图工作。如何更改我的字体在这种情况下?
奥凯我几乎没有...我需要访问我的资产,但我无法从我的自定义适配器。我试图用最后AssetManager资产= this.getAssets();
但不会得到我任何进一步的..
如何解决呢?
类Myadapter扩展了BaseAdapter {
LayoutInflater LIF;
ImageView的sideArrow;
TextView的电视;
公共Myadapter(上下文CTX){
LIF =(LayoutInflater)CTX
.getSystemService(LAYOUT_INFLATER_SERVICE);
}
@覆盖
公众诠释getCount将(){
返回favarets.size();
}
@覆盖
公共对象的getItem(INT位置){
返回的位置;
}
@覆盖
众长getItemId(INT位置){
返回的位置;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看VI = convertView;
如果(convertView == NULL)
VI = lif.inflate(R.layout.inflate,NULL);
sideArrow =(ImageView的)vi.findViewById(R.id.imageViewsidemark);
电视=(TextView中)vi.findViewById(R.id.textFav);
tv.setText(favarets.get(位置));
最后AssetManager资产= this.getAssets();
最后的字样tvFont = Typeface.createFromAsset(资产,OPTIMA.TTF);
tv.setTypeface(tvFont);
tv.setTextColor(Color.BLACK);
返回六;
我找到了解决办法:D
公共类Myadapter扩展了BaseAdapter {
AssetManager assetManager = getAssets();
LayoutInflater LIF;
ImageView的sideArrow;
TextView的电视;
公共Myadapter(上下文CTX){
LIF =(LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
}
@覆盖
公众诠释getCount将(){
返回favarets.size();
}
@覆盖
公共对象的getItem(INT位置){
返回的位置;
}
@覆盖
众长getItemId(INT位置){
返回的位置;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看VI = convertView;
如果(convertView == NULL)
VI = lif.inflate(R.layout.inflate,NULL);
sideArrow =(ImageView的)vi.findViewById(R.id.imageViewsidemark);
电视=(TextView中)vi.findViewById(R.id.textFav);
tv.setText(favarets.get(位置));
最后的字样tvFont = Typeface.createFromAsset(assetManager,OPTIMA.TTF);
tv.setTypeface(tvFont);
tv.setTextColor(Color.BLACK);
返回六;
}
}
I've found loads of different ways of accomplishing this but I'm not sure what's the best for my scenario.
This is my Java code for the listview:
ListView lv;
lv = (ListView) findViewById(R.id.favList);
This is the xml code for the list:
<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>
For a text view I would add:
final Typeface fontList = Typeface.createFromAsset(assets, "optima-extra-black.ttf");
lv.setTypeface(fontList);
But this doesn't work for listviews.How do I change my font in this case?
Oke I'm almost there...I need to access my assets but I can't from within my custom adapter.I tried using final AssetManager assets = this.getAssets();
but that won't get me any further..
How to tackle this?
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;
I found out the solution :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;
}
}
这篇关于Android的列表视图改变字体和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!