【问题标题】:change size of text of item of list view in .java [closed]在.java中更改列表视图项的文本大小[关闭]
【发布时间】:2016-12-29 11:31:07
【问题描述】:

我有一个活动 (main.xml & main.java) 和一个额外的 item.xml 文件来定义列表项,main.xml 包含 listView它的java文件是main.java。所以问题是如何更改 main.java 中 listItem 的文本大小? 如果 main.xml 包含一个文本(例如 t=(TextView)findViewById(R.id.text);),我们可以通过编写来更改它:

t.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);

但是如何在 main.java 中更改 item.xml 的文本大小?

【问题讨论】:

标签: java android


【解决方案1】:

我假设您使用的是适配器。覆盖适配器内部的 getView() 方法和这个:

TextView text = (TextView) listItemView.findViewById(R.id.text);
text.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);

如果你不明白,我在下面附上适配器内的示例覆盖 getView() 方法。

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View listItemView = convertView;
    if(listItemView == null)
    {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }

    EarthQuake currentEarthQuakeObj = getItem(position);

    TextView magText = (TextView) listItemView.findViewById(R.id.magnitude);
    magText.setText(Double.toString(currentEarthQuakeObj.getMagnitude()));

    return listItemView;
}

【讨论】:

  • 让您的样品尽可能小且干净。所有这些细节都会分散解决方案的注意力。
  • 感谢您的帮助...!为什么我在使用 Sharedpreference 作为 size 值时得到空指针:
  • 感谢您的帮助...!为什么我在使用 Sharedpreference 作为 size 值时得到空指针:TextView mode = (TextView) viewItem.findViewById(R.id.modeId); mode.setText(currentinfo.getMode()); mode.setTextSize(TypedValue.COMPLEX_UNIT_SP,sharedp.getInt("font_size",18)); m 得到空指针异常。
  • 我没有 SharedPreferences 方面的知识...所以我无能为力...抱歉
  • 不,不,它的 k...我声明了 Sharedpreference 但忘记初始化...抱歉..现在 ma 应用程序工作正常..非常感谢
猜你喜欢
  • 2013-12-25
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 2019-02-27
相关资源
最近更新 更多