android - 如何使用 simple_list_item_multiple_choice 换行文本?

标签 android textview listactivity word-wrap android-wrap-content

我正在使用 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>

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

最佳答案

有几个选项。

您可以为列表中的项目定义自定义布局。这允许您完全自定义您的项目 UI。更多详细信息,请参阅 List View tutorial .

如果您仍想使用标准项目布局但只修复其中的一个 事物,则可以使用另一种方法。您可以扩展 Adapter 并在 getView 函数中修改 View 。这是一个例子:

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

}

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

关于android - 如何使用 simple_list_item_multiple_choice 换行文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6225913/

相关文章:

Android IntentService 将数据返回给 Activity

Android CursorAdapter 不调用 newView

android - Jetpack Compose 脚手架 + 模态底板

android - Firebase Firestore 函数通知 Android

swift - 连字 SwiftUI 文本

java - Android TextView 在一段后切断

android - 如何修复 Html.fromHtml 链接焦点可见性问题(在 ICS 和 Honeycomb 中)?

android - 运行cordova build –release android时出错

javascript - Android 移动应用程序 : indoor maps, 步行路线

android - 如何将 registerOnSharedPreferenceChangeListener 添加到 ListActivity?