【问题标题】:How to dynamically set the text size in multiple textviews forming the rows of a list view?如何在形成列表视图行的多个文本视图中动态设置文本大小?
【发布时间】:2012-09-04 22:12:57
【问题描述】:

我已经看了几天了,但无济于事(还没有!)

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

我想在两种情况下一次性调整列表视图中所有“标签”文本视图中的文本大小: 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]);
    }
}

【问题讨论】:

  • 在生成ListView 时您的fontSize 是否发生变化?
  • 是的,在列表视图生成后字体大小会发生变化 - 列表视图应该相应地更新相同的项目但更大的字体大小。嗯……

标签: java android android-listview textview font-size


【解决方案1】:

在您按钮的 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();

    }
})

【讨论】:

  • 太好了,谢谢我不知道 notifyDataSetChanged() 方法,但它只是票。对于其他感兴趣的人,使用 setTextSize(TypedValue, value) 方法最终会更好地工作: int fontSize = spSettings.getInt("fontSize", 16); label.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
猜你喜欢
  • 2017-06-19
  • 2011-05-30
  • 1970-01-01
  • 2016-06-08
  • 1970-01-01
  • 1970-01-01
  • 2017-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多