我有一堂课,ListMenuAdapter
里面有:
ListMenuAdapter adapter = new ListMenuAdapter(this, R.layout.listmenu_item, listMenuData);
问题是:我无法从R.layout.listmenu_item
. 我需要访问它,因为我想更改它的字体。
这是完整的代码:
@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