android 实现一个简单纯文本的ListView

思维线路:

1.创建一个ListViewActivity,LinearLayout布局里写了一个ListView布局

2.创建一个TextView布局给ArrayAdapter适配器使用

3.将TextView布局和数据导入适配器ArrayAdapter

3.将ArrayAdapter适配好的内容导入ListView布局

代码如下:

ListViewActivity 的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/SimpleListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</LinearLayout>

TextView 的布局:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ListTextView"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_marginLeft="20dp"
    android:text="内容"
    android:textSize="50dp">


</TextView>

onCreate实现代码:

package com.example.prize.mylistviewdemoapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SimpleListView extends AppCompatActivity {
    private String [] data ={"苹果","橘子","芒果","香蕉","柠檬","火龙果","西瓜","李子",
            "芭乐","石榴","葡萄","荔枝","圣女果","杨梅","柿子","山竹","杨桃","雪梨","猕猴桃","榴莲"
            ,"枇杷","樱桃","柚子","水蜜桃","桑葚","莲雾"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_list_view);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(SimpleListView.this,R.layout.list_layout,data);//适配器
        ListView listView = (ListView) findViewById(R.id.SimpleListView); //找到ListView布局
        listView.setAdapter(adapter); //导入
    }
}

实现效果图:


猜你喜欢

转载自blog.csdn.net/qq_37217804/article/details/79886753