我正在使用 SimpleCursorAdapter
来显示单个 CheckedTextView
。我知道最好使用 simple_list_item_multiple_choice
和 android.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/