安卓学习第10课——listview
1.普通listview
<LinearLayout 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" android:orientation="vertical"> <ListView android:layout_width="wrap_content" android:layout_height="match_parent" android:entries="@array/books" android:divider="#f00" android:dividerHeight="2sp" android:background="#6E4A90" android:headerDividersEnabled="false" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="books"> <item >疯狂英语1</item> <item >疯狂英语2</item> <item >疯狂英语3</item> <item >疯狂英语4</item> <item >疯狂英语5</item> </string-array> </resources>
可以用xml创建数组。
2.用ArrayAdapter创建listview
<LinearLayout 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" android:orientation="vertical"> <ListView android:id="@+id/list1" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#f00" android:dividerHeight="2sp" android:headerDividersEnabled="false" /> <ListView android:id="@+id/list2" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#0f0" android:dividerHeight="2sp" android:headerDividersEnabled="false" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:padding="10sp" android:shadowColor="#f0f" android:shadowDx="4" android:shadowDy="4" android:shadowRadius="2" />
<?xml version="1.0" encoding="utf-8"?> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:padding="10sp" android:checkMark="@drawable/ok" android:shadowColor="#f0f" android:shadowDx="4" android:shadowDy="4" android:shadowRadius="2" />
package com.example.arrayadapter; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView list1=(ListView) findViewById(R.id.list1); String[] arr1={"孙悟空","猪八戒","牛魔王"}; ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this,R.layout.array_item,arr1); list1.setAdapter(adapter1); ListView list2=(ListView) findViewById(R.id.list2); String[] arr2={"java","spring","android"}; ArrayAdapter<String> adapter2=new ArrayAdapter<String>(this,R.layout.checked_item,arr2); list2.setAdapter(adapter2); } }
注》疑问,checkMark用法
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。