更新项目高度的ListView修改文本大小时 [英] Update item height when modifying text size in ListView

查看:177
本文介绍了更新项目高度的ListView修改文本大小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView ArrayAdapter 它使用一个XML布局,类似于simple_list_item_1管理,以夸大其内容。我在应用程序菜单中两个选项,增加/减小文本大小。我设法修改的 TextViews 中的的ListView 通过覆盖 getView <文本大小/ code>的 ArrayAdapter 功能。事情是这样的:

I have a ListView with an ArrayAdapter which uses an XML layout, similar to simple_list_item_1, to inflate its content. I have two options in the app menu to increase/decrease the text size. I managed to modify the text size of the TextViews inside the ListView by overriding the getView function of the ArrayAdapter. Something like this:

adapter = new ArrayAdapter<Line>(MyActivity.this, R.layout.line_list, lines){
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView = (TextView) super.getView(position, convertView, parent);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSizeDp + zoom);
        return textView;
    }
};
listView.setAdapter(adapter);

选项改变变焦值,并调用 adapter.notifyDataSetChanged()的菜单; 来使适配器改变文字大小。
直到这一点的一切工作。

The menu options change the zoom value and call adapter.notifyDataSetChanged(); to make the adapter change the text size. Until that point everything works.

现在的问题是,当我缩小文本大小太多,每个TextView的高度不包其内容。也就是说,ListView的项目是太高。当我增加文字大小,我没有与项目的高度问题。

The problem is that when I decrease the text size too much, the height of each TextView is not wrapping its content. That is, the ListView items are too high. When I increase the text size I don't have problems with the items height.

如果我叫super.getView(位置,convertView,父母);与convertView是空的,我做适配器八方通膨胀。在这种情况下,降低的文本大小,因为高度缠绕工作正常。不过,我的工作造成的过载,我不希望出现这种情况。

If I call super.getView(position, convertView, parent); with convertView being null, I make the adapter to allways inflate. In that case, decreasing the text size works fine since the height is wrapped. However, I'm causing an overload of work and I don't want that.

我试过无效或者强迫布局到ListView和/或textviews,但没有奏效。

I tried invalidating or forcing layout to the listview and/or the textviews but it didn't work.

任何帮助是pciated AP $ P $

Any help is appreciated

编辑:这是line_list布局:

This is line_list layout:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="@dimen/lineTextSize"
android:gravity="center"
android:paddingLeft="6dip"
android:paddingRight="6dip" />

lineTextSize是默认和40dp为大型22dp

lineTextSize is 22dp for default and 40dp for large

编辑2:这个样子

EDIT 2: Look like this

而不是这个画面:

instead of this screen:

推荐答案

我发现<一个href=\"http://stackoverflow.com/questions/9541196/androidtextview-height-doesnt-change-after-shrinking-the-font-size\">here这是Android 3.1的一个已知错误+

I found here that this is a known bug for Android 3.1+

的解决方案是增加这样的字符串

The solution is to add a string like this

String ZERO_WIDTH_SPACE = "\u200b";

到TextViews的文本。因此,适配器的正确 getView 的功能是:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView textView = (TextView) super.getView(position, convertView, parent);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSizeDp + zoom);
    textView.setText(textView.getText() + ZERO_WIDTH_SPACE);
    return textView;
}

这篇关于更新项目高度的ListView修改文本大小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆