【发布时间】:2016-09-07 04:07:12
【问题描述】:
我一直在阅读 simple_list_item_2 和其他在列表视图上显示多行文本的标准化方法,但没有一个给我我想要的东西。到目前为止,我正在为列表视图使用自定义布局,但我缺乏关于如何自定义列表视图中的每一行以显示它自己的大文本行、工具提示(小文本)和图片的知识(在阅读了多篇文章之后) .
这是我用来初始化列表视图和设置适配器的代码
//List View
final String[] itemname = {
"More Rockets",
"Better Lasers",
"Super Shield",
"Super Defense"
};
final String[] itemtip = {
"+1 attack",
"+5 attack ",
"+1 defense",
"+5 defense"
};
final ListView upgradeList = (ListView) findViewById(R.id.upgradeListView);
upgradeList.setAdapter(new ArrayAdapter<String>(
this, R.layout.program_list,
R.id.Itemname, itemname));
program_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="80dp"
android:background="@drawable/buttontemplate">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Item Name"
android:id="@+id/Itemname"
android:textSize="30sp"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/imageView"
android:layout_marginLeft="15sp" />
<ImageView
android:layout_width="30sp"
android:layout_height="30sp"
android:id="@+id/imageView"
android:src="@drawable/heart"
android:layout_marginLeft="20dp"
android:layout_alignBottom="@+id/Itemname"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Tooltip:"
android:id="@+id/toolTip"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/Itemname"
android:layout_marginLeft="20dp" />
</RelativeLayout>
这是我在列表视图中添加另一行文本的想法,但它不是正确数量的参数
upgradeList.setAdapter(new ArrayAdapter<String>(
this, R.layout.program_list,
R.id.Itemname, itemname, R.id.toolTip, itemtip));
非常感谢任何帮助! -凯尔顿
【问题讨论】:
-
你为什么不用
BaseAdapter?在那里,您将对适配器和布局有更多的控制。我认为这样你会得到你想要的 -
@Marat 我不知道这是一件事,但我调查了一下,它看起来正是我需要的。谢谢!
-
很高兴知道这一点。你的
CustomAdapterClass应该看起来像这样:stackoverflow.com/a/37783784/6272369 然后尝试使用 TextView 的 xml 属性,如lines或maxLines以获得更好的外观。你可以在这里找到它的材料developer.android.com/reference/android/widget/TextView.html
标签: java android listview android-studio