java - 如何动态设置形成 ListView 行的多个 TextView 中的文本大小?

标签 java android android-listview textview font-size

我已经研究了几天了,但没有结果(还!)

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

我想一次性调整 ListView 中所有“标签” TextView 中文本的大小,有两种情况: 1)响应当前 Activity 中的按钮单击 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]);
    }
}

最佳答案

在按钮的 onClickListener 中,将新字体大小保存到共享首选项中,然后使用 notificationDataSetChanged 方法。我在想这样的事情。

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();

    }
})

关于java - 如何动态设置形成 ListView 行的多个 TextView 中的文本大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12378896/

相关文章:

java 通用ftpclient上传文本文件

android - 无法根据 android 中的给定文本对齐可绘制矩形坐标?

android - 如何将公共(public)方法变量的值设置为 protected 方法中的变量?

java - 谷歌应用引擎抛出未知错误 : '4' all of a sudden

java - java中的setIconImage不工作

java - 检查对象是否与此属于同一类?

java - Facebook session 未打开

android - 我如何将虚拟条目添加到 listView

android - 使用 SimpleAdapter 过滤 listView 数据

java - 如何拥有触摸监听器并且仍然能够滚动