【问题标题】:Displaying multiple lines of text on a listview在列表视图上显示多行文本
【发布时间】: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 属性,如 linesmaxLines 以获得更好的外观。你可以在这里找到它的材料developer.android.com/reference/android/widget/TextView.html

标签: java android listview android-studio


【解决方案1】:

首先制作一个带有字段String itemname,String itemtip的POGO类,然后生成getter和setter方法,然后从activity类中制作这个pogo类的arraylist,即

ArrayList<MyPOGO> arraylist =new ArrayList();
MyPOGO myPogo1=new MyPogo("More Rockets","+1 attack");
MyPOGO myPogo2=new MyPogo("Better Lasers","+5 attack ");
MyPOGO myPogo3=new MyPogo("Super Shield","+1 defense");
MyPOGO myPogo4=new MyPogo("Super Defense", "+5 defense");
arraylist.add(myPogo1);
arraylist.add(myPogo2);
arraylist.add(myPogo3);
arraylist.add(myPogo);

这里你的数组列表大小是 4 。 现在让 baseadapter 或 recyler 视图传递这个数组列表

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2011-10-03
    • 2014-11-21
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多