0

我有一堂课,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

4

3 回答 3

1

您可以在 Adapter 的 getView 方法中获取对 textview 的引用。

holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

在此之后,您可以设置字体

于 2013-01-05T14:19:44.497 回答
1
holder.txtTitle.setText(menu.title);

在此之后在 getview 中尝试

holder.txtTitle.setTypeFace(face);
于 2013-01-05T14:22:42.200 回答
1

作为我对您的问题留下的评论,您可以在getView()方法中为标题 textview 设置 typeFace,holder.txtTitle.setText(menu.title);如下所示:

要从资产中获取字体实例,您需要将 Context 参数传递给您的适配器 - 我认为您已经这样做了 - 然后使用该实例创建字体,就像您在您的onCreate()或检查下面的编辑中所做的那样:

编辑

Typeface customFont = Typeface.createFromAsset(getContext().getAssets(),"Androgyne_TB.otf");

然后你可以设置 typeFace 如下:

holder.txtTitle.setTypeFace(customFont);

于 2013-01-05T14:24:21.487 回答