2011-05-19 89 views
0

我试图在我的应用程序中更改textSize。 我想改变我的customlistview的textSize。 我的listview的行XML文件有图像,3个textView。 我想在用户单击Optionmenu时更改textView的textSize,然后单击AlertDialog的SingleChoiceItems复选框。如何更改自定义列表视图中的文本大小(字体大小)?

这是我的代码。

我需要改变XML文件?.. 那我该怎么设置我的customAdapter?.. 我需要改变我的适配器的getView方法?

我接受任何答案。

public boolean onOptionsItemSelected(MenuItem item){ 
    switch(item.getItemId()){ 

     case R.id.ks_notice_menu_textsize: 

      final CharSequence[] items = {"normal", "big", "bigger"}; 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setTitle("select textSize"); 
      builder.setSingleChoiceItems(items, mSelect, 
        new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        mSelect = which; 
       } 
      }); 

      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 



        int textSize = (int)Username.getTextSize(); 
        switch(mSelect){ 
        case 0:  

         //here I need to change textSize 

         break; 
        case 1: 
         //here I need to change textSize 
         break;       


        case 2: 
         //here I need to change textSize 
         break; 
        } 



       } 
      }); 
      builder.setNegativeButton("cancel", null); 
      builder.show(); 
      return true;  
    } 
    return false; 

回答

0

在你的XML为ListView的行,你需要的ID在问题添加到TextView秒。然后,你需要更换:

//here I need to change textSize 

(假设你想的5文字大小):

customAdapter.setTextSize(5); 

最后,在你的customAdapter您需要实现一个setTextSize(int)方法检索所有查看并将其文本大小设置为传递的值。它还需要存储该值,并将其用于在用户向下滚动时创建的任何新视图。

喜欢的东西:

public void setTextSize(final int textSize) { 
    fTextSize = textSize; 

    for (View view : fViews) { 
     ((TextView) view.findViewById(R.id.list_view_text_1).setTextSize(textSize); 
     ((TextView) view.findViewById(R.id.list_view_text_2).setTextSize(textSize); 
     ((TextView) view.findViewById(R.id.list_view_text_3).setTextSize(textSize); 
    } 
} 

和:

public View getView(final int position, final View convertView, final ViewGroup parent) { 
    View view = // retrieve your view from XML. 
    fViews.add(view); 
    ((TextView) view.findViewById(R.id.list_view_text_1).setTextSize(textSize); 
    ((TextView) view.findViewById(R.id.list_view_text_2).setTextSize(textSize); 
    ((TextView) view.findViewById(R.id.list_view_text_3).setTextSize(textSize); 

    return (view); 
} 
相关问题