首页 > 代码库 > 安卓学习第10课——listview
安卓学习第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用法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。