简体   繁体   English

如何自定义Android ListView的每一行?

[英]How can I customize each row of Android ListView?

I would like to know if it's possible to customize each row of listview? 我想知道是否可以自定义列表视图的每一行? I want to add checkboxes to few of the rows, button for another, etc. How to build *.xml file for this? 我想在少数行中添加复选框,为另一行添加按钮,等等。如何为此构建* .xml文件?

Thanks in advance! 提前致谢!

You can write a normal layout file like the following: 您可以编写一个普通的布局文件,如下所示:

<?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>

Then in your listview adapter function 然后在你的listview适配器功能

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

you can use the line 你可以用线

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

This is what i have done. 这就是我所做的。

RFTM Android custom components and binding data to a view RFTM Android自定义组件并将数据绑定到视图

That leads to creating a custom Adapter (probably from ListAdapter or ArrayAdapter ). 这导致创建自定义适配器(可能来自ListAdapterArrayAdapter )。

You can also start with a ListActivity. 您也可以从ListActivity开始。

Yeah!It's possible to customize each row with a your layout.(also with CheckBox) You need to develop a custom ArrayAdapter. 是的!可以使用您的布局自定义每一行。(也可以使用CheckBox)您需要开发自定义ArrayAdapter。 You may find a tutorial here : 您可以在这里找到教程:

I hope it will be useful for you. 希望对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM