1

我有一个ListViewArrayAdapter,它使用类似于simple_list_item_1的XML布局来扩充其内容。我在应用程序菜单中有两个选项可以增加/减少文本大小。我设法通过覆盖ArrayAdaptergetView函数来修改ListView内的TextViews的文本大小。事情是这样的:在ListView中修改文本大小时更新项目高度

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();,使适配器改变文字大小。 直到这一点一切正常。

问题是,当我减少太多的文本大小时,每个TextView的高度都没有包裹它的内容。也就是说,ListView项目太高。当我增加文字大小时,我不会遇到与物品高度有关的问题。

如果我调用super.getView(position,convertView,parent);与convertView为空,我使适配器allways充气。在这种情况下,由于高度被包裹,减小文本大小可以正常工作。但是,我造成了工作负担过重,我不想那样做。

我试图无效或强制布局到列表视图和/或文字浏览,但它没有奏效。

任何帮助表示赞赏

编辑:这是line_list布局:

<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大型

编辑2 22dp:这个样子enter image description here

而不是此scre EN:enter image description here

+0

你能后的'R.layout.line_list'文件? – Luksprog

+0

我编辑了原帖 – IvanRF

+0

我以前测试过你的代码,它的工作原理应该如此,无论我如何修改大小,'TextView'都会包装'TextView'的内容。在我的测试中'fontSizeDp'是常量,像17,我简单地增加/减少了'onOptionsItemSelected'回调中的'zoom'变量,然后在适配器上调用'notifyDataSetChanged'。你如何修改尺寸? – Luksprog

回答

1

我发现here这是Android 3的一个已知的错误。1+

的解决方案是这样的

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; 
} 
0

我设法测试代码,它似乎ListView不希望更新的行的高度Honeycomb及以上版本(减小字体大小时)。解决您的问题的一个简单方法是以适当方式实施getView方法,以便在字体减少和/或回收视图不适合当前字体大小的情况下重新创建行视图。这是基于你的观察,强制super.getView()方法与convertView设置为null工程,因为ListView将始终构建新的行从头开始。我的代码是做类似的事情只能用更好的性能:

public class CustomAdapter extends ArrayAdapter<String> { 

    private LayoutInflater mInflater; 
    private int mResource; 

    public CustomAdapter(Context context, int resource, String[] objects) { 
     super(context, resource, objects); 
     mInflater = LayoutInflater.from(context); 
     mResource = resource; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView = mInflater.inflate(mResource, parent, false); 
     } else { 
      if (mZoom - mOldZoom < 0) { 
       Integer cacheZoom = (Integer) convertView.getTag(); 
       if (cacheZoom == null || cacheZoom.intValue() != mZoom) { 
        convertView = mInflater.inflate(mResource, parent, false); 
        convertView.setTag(mZoom); 
       } 
      } 
     } 
     ((TextView) convertView).setTextSize(
        TypedValue.COMPLEX_UNIT_DIP, 40 + mZoom); 
     ((TextView) convertView).setText(getItem(position)); 
     return convertView; 
    } 

} 

mZoommOldZoom与新和使用,看哪个方向的字体标题老变焦值的变量:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.item1: 
     mOldZoom = mZoom; 
     mZoom += 2; 
     mAdapter.notifyDataSetChanged(); 
     break; 
    case R.id.item2: 
     mOldZoom = mZoom; 
     mZoom -= 2; 
     mAdapter.notifyDataSetChanged(); 
     break; 
    } 
    return super.onOptionsItemSelected(item); 
} 
+0

R.layout.adapters_fontchange是什么?如果是与R.layout.line_list相同的布局,问题仍然存在(我正在测试ICS)。 – IvanRF

+0

@Ivan'R.layout.adapters_fontchange'是我的名字为你的布局文件(它是相同的布局)。我在'ICS'上测试过,实际上它不起作用,因为我的初始逻辑是错误的。我编辑了我的答案中的代码,它应该在我用2.2版设备和两个带有'Honeycomb'(API 13)+'ICS'(API 14)的模拟器进行测试时知道。看看它的行为。 – Luksprog

+0

它的工作原理,谢谢!只有一个错误,但可以通过删除'if(mZoom!= 0)'来解决,因为如果你减小一个,然后增加一个,因为mZoom是0,大小没有设置,并且文本的行为是磨损的,因为文本大小的38和40. – IvanRF