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

本文通过一个简单的实例展示了如何在Android应用中使用ListView组件。具体步骤包括创建ListViewActivity、定义ListView布局、制作TextView布局作为适配器模板,并使用ArrayAdapter将字符串数组数据绑定到ListView上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思维线路:

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); //导入
    }
}

实现效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值