我的课程里面有一个ListMenuAdapter
:
ListMenuAdapter adapter = new ListMenuAdapter(this, R.layout.listmenu_item, listMenuData);
问题是:我无法从R.layout.listmenu_item
访问textview。我需要访问它,因为我想更改其字体。
这是完整的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListMenu listMenuData[] = new ListMenu[]
{
new ListMenu(R.drawable.final_test, "Hello World ^_^", getHighScore(1)),
new ListMenu(R.drawable.introduction, "Introduction", getHighScore(1)),
new ListMenu(R.drawable.input_output, "Input - Output", getHighScore(2))
};
ListMenuAdapter adapter = new ListMenuAdapter(this, R.layout.listmenu_item, listMenuData);
listMenu = (ListView)findViewById(R.id.listMenu);
View header = (View)getLayoutInflater().inflate(R.layout.listmenu_header, null);
//I TRIED THIS FOR ACCESSING THE TEXTVIEW, BUT NO LUCK
View titles = (View)getLayoutInflater().inflate(R.layout.listmenu_item, null);
Typeface customFont = Typeface.createFromAsset(this.getAssets(), "Androgyne_TB.otf");
//WORKING
txtHeader = (TextView)header.findViewById(R.id.txtHeader);
//NOT WORKING
txtTitle = (TextView) titles.findViewById(R.id.txtTitle);
//WORKING
txtHeader.setTypeface(customFont);
//NOT WORKING
txtTitle.setTypeface(customFont);
listMenu.addHeaderView(header);
listMenu.setAdapter(adapter);
listMenu.setOnItemClickListener(this);
}
我试图通过R.layout.listmenu_item
来充分访问Textview:
View titles = (View)getLayoutInflater().inflate(R.layout.listmenu_item, null);
但它仍无效(请参阅上面的完整代码):D
已编辑:
这是ListMenuAdapter
GetView
方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
MenuHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MenuHolder();
holder.imgMenuIcon = (ImageView)row.findViewById(R.id.imgMenuIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
//holder.txtHighScore = (TextView)row.findViewById(R.id.txtHighScore);
//holder.txtHighScore.setTextColor(Color.RED);
holder.rtbHighScore = (RatingBar) row.findViewById(R.id.rtbHighScore);
//holder.rtbHighScore.setNumStars(5);
//holder.rtbHighScore.setEnabled(false);
row.setTag(holder);
}
else holder = (MenuHolder)row.getTag();
ListMenu menu = listmenu[position];
holder.imgMenuIcon.setImageResource(menu.icon);
holder.txtTitle.setText(menu.title);
//holder.txtHighScore.setText("High Score : " + menu.highscore);
holder.rtbHighScore.setRating(menu.highscore/20);
return row;
}
感谢您的帮助:D
答案 0 :(得分:1)
您可以在适配器的getView方法中获得对textview的引用。
holder.txtTitle =(TextView)row.findViewById(R.id.txtTitle);
在此之后,您可以设置字体
答案 1 :(得分:1)
holder.txtTitle.setText(menu.title);
在getview尝试之后
holder.txtTitle.setTypeFace(face);
答案 2 :(得分:1)
作为我在您的问题上留下的评论,您可以在getView()
行下的holder.txtTitle.setText(menu.title);
方法中为标题文字视图设置typeFace,如下所示:
要获取字体实例表单Assets,您需要将Context参数传递给您的适配器 - 我认为您已经这样做了 - 然后使用该实例创建字体,就像在onCreate()
中一样或检查编辑如下:
修改强>
Typeface customFont = Typeface.createFromAsset(getContext().getAssets(),"Androgyne_TB.otf")
;
然后你可以将typeFace设置如下:
holder.txtTitle.setTypeFace(customFont);