gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:05:16 24 4
gpt4 key购买 nike

这几天我一直在研究这个问题,但仍然无济于事(至今!)

正如问题所说 - 我正在尝试动态更改 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 中将新字体大小保存到您的共享首选项,然后使用 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();

}
})

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com