05-17 列表视图

1.AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.administrator.testapp">
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:supportsRtl="true"
10         android:theme="@style/AppTheme">
11         <activity android:name=".MainActivity" />
12         <activity android:name=".test_activity6"></activity>
13         <activity android:name=".TestActivity7">
14             <intent-filter>
15                 <action android:name="android.intent.action.MAIN" />
16                 <category android:name="android.intent.category.LAUNCHER" />
17             </intent-filter>
18         </activity>
19         <activity android:name=".TestActivity8"></activity>
20     </application>
21 
22 </manifest>

2.activity_test7.xml

【列表视图布局文件】

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     tools:context=".TestActivity7"
 7     android:orientation="vertical">
 8 
 9     <ListView
10         android:layout_width="match_parent"
11         android:layout_height="match_parent"
12         android:id="@+id/lv_1">
13     </ListView>
14 </LinearLayout>

3.列表视图代码运行程序

 1 package com.example.administrator.testapp;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.widget.ArrayAdapter;
 6 import android.widget.ListView;
 7 
 8 public class TestActivity7 extends AppCompatActivity {
 9 
10     ListView lv_1;
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_test7);
16 
17         ListView lv_1 = (ListView)findViewById(R.id.lv_1);
18 
19         //1-1.数据集合 layout文件
20         String [] strings = {"A1","A2","A3","A4","A5","A6","A7","A8","A9","A10",
21                 "A11","A12","A13","A14","A15","A16","A17","A18","A19","A20"};
22         //1-2.创建适配器 AdaPter
23         ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.array_adapter,strings);
24         //1-3.绑定到ListView
25         lv_1.setAdapter(arrayAdapter);
26     }
27 }

4.array_adapter.xml

1 <?xml version="1.0" encoding="utf-8"?>
2     <TextView
3     xmlns:android="http://schemas.android.com/apk/res/android"
4         android:layout_width="match_parent"
5         android:layout_height="wrap_content"
6         android:textSize="20sp"
7         android:paddingTop="10dp"
8         android:paddingBottom="10dp"
9     />

原文地址:https://www.cnblogs.com/TENOKAWA/p/5502986.html