2011-06-03 59 views
4

我使用SimpleCursorAdapter来显示单个CheckedTextView。我知道这是最好的使用simple_list_item_multiple_choiceandroid.R.id.text1使用simple_list_item_multiple_choice环绕文本

adapter = new SimpleCursorAdapter(getApplicationContext(), 
       android.R.layout.simple_list_item_multiple_choice, rules, 
       new String[]{Constants.KEY_RULE}, new int[]{android.R.id.text1}); 

如果从KEY_RULE文字超过两行,Android不换,而是隐藏它。是否有一个简单的解决方法,而不必实现我自己的适配器?

找到我下面的XML代码:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:id="@+id/header" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:text="@string/rule_selection_for_revision_list_title" 
     android:padding="4dip" android:background="#000000" android:textColor="#ffffff" android:textStyle="bold" /> 

    <ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_weight="1.0" /> 

    <TextView android:textSize="18sp" android:textStyle="bold" 
     android:id="@id/android:empty" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal" 
     android:text="There are no rules to consider." /> 

</LinearLayout> 

有什么办法,我至少可以减少字体大小,使其适合于两行呢?

回答

2

有几种选择。

您可以在列表中定义项目的自定义布局。这使您可以完全自定义您的项目用户界面。更多细节可在List View tutorial中找到。

如果您仍然想要使用标准项目布局,但只能修复其中一个小事,则可以使用其他方法。您可以扩展适配器并修改getView函数中的视图。这里是一个例子:

class MySimpleCursorAdapter extends SimpleCursorAdapter 
{ 
    public MySimpleCursorAdapter(Context ctx, int layout, 
           Cursor cursor, String [] from, int [] to) 
    { 
     super(ctx, layout, cursor, from, to); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     View view = super.getView(position, convertView, parent); 

     TextView textView = (TextView)view.findViewById(android.R.id.text1); 

     //You can increase number of lines in the text view 
     textView.setMaxLines(5); 

     LayoutParams params = textView.getLayoutParams(); 
     if(params.height > 0) 
     { 
      int height = params.height; 
      params.height = LayoutParams.WRAP_CONTENT;        
      textView.setLayoutParams(params); 
      textView.setMinHeight(height); 
     } 

     //Or you can make your font smaller 
     textView.setTextSize(customTextSize); 

     //or whatever you like, even apply new custom style 
     //... 

     return view; 
    } 

} 

您应该选择第一种方法(自定义布局),但有时第二种方法也是可行的。

+0

第二种方法更适合我,我认为。唯一的问题是MaxLines不起作用。 setTextSize虽然工作,但不是一个整洁的解决方案。是不是有什么办法可以告诉文本视图来包装它的内容并自动增加而不是有一个静态高度? – kkudi 2011-06-03 11:20:12

+0

'setMaxLines(N)'应该做到这一点。它会将'TextView'的大小增加到N行。布局是'TextView'的位置也可能会限制高度。让我检查一下。 – inazaruk 2011-06-03 11:22:09

+0

好的,如果您查看http://codesearch.google.com/codesearch/p?hl=zh_CN#uX1GffpyOZk/core/res/res/layout/simple_list_item_multiple_choice.xml&q=layout.simple_list_item_multiple_choice&sa=N&cd=1&ct=rc,您会注意到android:layout_height被设置为特定的高度(使用当前主题值)。这可以在您的代码中重写。我已经更新了答案,以显示如何设置“TextView”的高度。 – inazaruk 2011-06-03 11:29:45

0

您不需要实现自定义适配器。只需为列表项定义一个自定义布局,并将其传递给适配器而不是android.R.layout.simple_list_item_multiple_choice

您可以搜索Android源代码(与计算机上的SDK一起提供),以了解原始simple_list_item_multiple_choice布局的外观,并根据您的需要进行调整。