2012-09-11 153 views
2

我一直在寻找这几天现在,但无济于事(还!)如何在多个文本视图中动态设置文本大小,形成列表视图的行?

正如问题所说 - 我试图动态地改变显示在列表视图中的文本的大小。我的xml设置为将listview中的每一行描述为具有图像视图(图标)和文本视图(标签)。

我想调整在列表视图中所有的“标签” textviews内文本的大小一气呵成,在两种情况下:响应 1)点击一个按钮,在应对当前的活动 2)到从共享偏好读取的值

我相信我可以使用setTextAppearance方法。这是我的代码 - 它运行时没有错误,但它也没有预期的效果!

我会非常感谢您对此有任何想法。最良好的祝愿史蒂芬

import android.content.Context; 
import android.content.SharedPreferences; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class IndexCustomAdapter extends ArrayAdapter<String> 
{ 
LayoutInflater inflater; 
String[] indexContents; 
String[] scores; 

private SharedPreferences spSettings; 

public IndexCustomAdapter(Context context, int indexRowId, String[] objs) 
{ 
    super(context, indexRowId, objs); 
    inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    indexContents = objs; 
    spSettings = getContext().getSharedPreferences("settings", 0); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    if (convertView == null) 
    { 
     convertView = inflater.inflate(R.layout.indexrow, parent, false); 
    } 

    TextView label = (TextView) convertView.findViewById(R.id.label); 
    ImageView icon = (ImageView) convertView.findViewById(R.id.icon); 

    // Select the correct text size 
    int fontSize = spSettings.getInt("fontSize", 16); 
    switch (fontSize) 
    { 
     case 24: 
      label.setTextAppearance(getContext(), android.R.attr.textAppearanceLarge); 
      break; 
     case 20: 
      label.setTextAppearance(getContext(), android.R.attr.textAppearanceMedium); 
     case 16: 
      label.setTextAppearance(getContext(), android.R.attr.textAppearanceSmall); 
    }  

    label.setText(indexContents[position]); 
    } 
} 
+0

你的'fontSize'在生成'ListView'时是否改变? – slybloty

+0

是的,在生成列表视图后字体大小发生了变化 - 并且列表视图应该相应地更新相同的项目,但更大的字体大小。嗯... – Steven

回答

2

在您的按钮的onClickListener新的字体大小保存到共享偏好,然后使用notifyDataSetChanged方法。我在想这样的事情。

button.SetOnclickListener(new OnclickListener(){ 
    public void onClick(View v){ 
     //Update shared preferences with desired 
     //font size here 

     //Instance of your IndexCustomAdapter that you attatched to your listview 
     indexCustomAdapter.notifyDataSetChanged(); 

    } 
}) 
+1

太好了,谢谢我没有意识到notifyDataSetChanged()方法,但它只是票。对于任何感兴趣的人来说,使用setTextSize(TypedValue,value)方法最终会更好地工作: int fontSize = spSettings.getInt(“fontSize”,16); label.setTextSize(TypedValue.COMPLEX_UNIT_DIP,fontSize); – Steven

相关问题