【问题标题】:How can I customize each row of Android ListView?如何自定义 Android ListView 的每一行?
【发布时间】:2011-12-14 12:21:59
【问题描述】:

我想知道是否可以自定义列表视图的每一行?我想为几行添加复选框,为另一行添加按钮等。如何为此构建 *.xml 文件?

提前致谢!

【问题讨论】:

标签: java android xml listview customization


【解决方案1】:

您可以编写一个普通的布局文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:id="@+id/lineItem">
  <!-- The attribute name -->
  <TextView android:id="@+id/attributeName" android:layout_height="wrap_content"
  android:layout_width="wrap_content" android:layout_alignParentLeft="true"
  android:textStyle = "bold" android:paddingTop = "2dp" android:textSize="16dp"/>

  <!-- The attribute value just beneath the attribute name -->
  <TextView android:id="@+id/attributeValue" android:layout_height="wrap_content"
  android:layout_width="wrap_content" android:layout_alignParentLeft="true"
  android:layout_below="@id/attributeName" android:textSize="12dp"  android:paddingBottom="2dp" />

  <!-- Now create button besides the line with right align -->
  <Button android:layout_width="wrap_content" 
  android:background="@drawable/printbutton" 
  android:layout_height="wrap_content"
  android:id="@+id/printbtn" 
  android:layout_centerInParent="true" android:layout_alignParentBottom="true"></Button>
</RelativeLayout>

然后在你的列表视图适配器函数中

public View getView(final int position, View convertView, ViewGroup parent)

你可以使用这条线

convertView = mInflater.inflate(R.layout.listviewrow,null);

这就是我所做的。

【讨论】:

    【解决方案2】:

    RFTM Android custom componentsbinding data to a view

    这会导致创建一个自定义适配器(可能来自ListAdapterArrayAdapter)。

    您也可以从 ListActivity 开始。

    【讨论】:

      【解决方案3】:

      是的!可以使用您的布局自定义每一行。(也可以使用 CheckBox) 您需要开发一个自定义的 ArrayAdapter。 你可以找到教程here

      希望对你有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多