如何使用simple_list_item_multiple_choice包装文本?

问题描述 投票:4回答:2

我正在使用SimpleCursorAdapter来显示单个CheckedTextView。我知道使用simple_list_item_multiple_choiceandroid.R.id.text1做得最好。

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>

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

android textview listactivity textwrapping android-wrap-content
2个回答
2
投票

有几种选择。

您可以为列表中的项目定义自定义布局。这允许您完全自定义项目UI。更多细节可以在List View tutorial找到。

如果您仍想使用标准项目布局但只修复其中的一小部分,则可以使用另一种方法。您可以扩展Adapter并修改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
投票

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

您可以搜索Android源(在计算机上附带SDK)以了解原始simple_list_item_multiple_choice布局的外观并根据您的需要进行调整。

© www.soinside.com 2019 - 2024. All rights reserved.