【问题标题】:Styling the android listview with array list adapter使用数组列表适配器样式化 android listview
【发布时间】:2026-01-28 20:50:01
【问题描述】:

我已经编写了从下面显示的一些数组中创建列表的代码! 代码运行正常,输出符合预期!

有相同问题的人更新: nice tutorial for custom listview

MainActivity.java

public class MainActivity extends Activity {

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   ListView listView1 = (ListView) findViewById(R.id.listView1);

    String[] items = { "some", "fancy", "items", "to", "show" };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.listitem, items);

    listView1.setAdapter(adapter);
   }

activity_main.xml

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   tools:context=".MainActivity" >

  <ListView 
    android:id="@+id/listView1" 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" />

   </RelativeLayout>

listview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:textSize="20sp"
android:padding="22dp"
android:gravity="center_horizontal"
/>

我想完成什么?

  1. 将每个列表项中文本的颜色和字体更改为不同的......并在点击它们时执行一些任务......

  2. 也可以在同一个列表视图中获取另一个列表视图 例如。如果我单击一个列表项,它会再次向我显示一个列表(一种子列表),在同一活动(或屏幕)上具有不同的列表项。点击子列表项可以执行一些操作。

感谢详细的答案,因为我是 android 开发的新手。 谢谢!

【问题讨论】:

  • 你说的是自定义ListView吗?
  • 是的!一个可以轻松定制并与 arrayadapter 一起使用的 listView
  • 看看我的回答。
  • if >>>自定义ListView.?是一个链接它坏了!
  • 在评论中。?不,它不是一个链接,我在下面的答案中提供了一个链接。

标签: java android listview styles


【解决方案1】:
1.change color and font of the text in each list item to a different one..and do some task on 
  tapping on them...
  • 制作一个自定义适配器,覆盖该适配器的getVIew() 并制作 颜色和文字的变化。
  • 为您的 ListView 覆盖 onItemClick()。完成点击事件 用于列表项。

现在

2. also is it possible to get another listview inside the same listview eg. if I click on a
   list item it again shows me a list (kind of a sub list) with different list items on that
   same activity(or screen).and some action could be done on tapping the sub list items.

3. what are my other list styling options..

还有Tutorial

【讨论】:

  • 您能否提供一个简单的示例代码或将我引至包含该示例的站点!谷歌搜索没有为我提供可以理解的东西
【解决方案2】:

您需要查看自定义 ArrayAdapter,如下所示 http://www.vogella.com/articles/AndroidListView/article.html

这将解决第一个和第三个问题。至于第二个,ListView 的默认实现是不可能的,但是有一些库可以让您创建下拉列表项。

【讨论】:

    【解决方案3】:

    对于第一个问题,您可以使用 paulthom12345' 的答案。

    第二个问题:你必须使用exapandableListView

    更多详情请见:Android ExpandableListView - Looking for a tutorial

    第三个问题不具建设性且非常模糊。请编辑问题并更详细地解释。

    【讨论】:

      【解决方案4】:
      1. 使用Customized List View

      2. 列表中不可能有一个列表。取而代之的是expandable list

      3. 看看Android Listview with different layout for each row

      【讨论】: