ArrayAdapter创建ListView

shadowDX、shadowDy、shadowRadius,说明分别是阴影的横、纵坐标偏移,以及阴影的半径
链接https://blog.csdn.net/u013571833/article/details/53582171
使用ArrayAdapter创建ListView

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
  //设置红色的线
 <ListView
     android:id="@+id/list1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:divider="#f00"
     android:dividerHeight="1dp"
     android:headerDividersEnabled="false"
     />
     <!--设置绿色分割线-->
  <ListView
      android:id="@+id/list2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:divider="#0f0"
      android:dividerHeight="1dp"
      android:headerDividersEnabled="false"
    />

</LinearLayout>
QQ截图20191011160205.png

两个ListView都没有定义abdroid:entries,所以都用adapter来提供选项
java程序:

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView list1=findViewById(R.id.list1);
        String[] arr1=new String[]{"孙悟空","猪八戒","牛魔王"};
        //将数组报装成Arrayadapter
        ArrayAdapter adapter1=new ArrayAdapter(this,R.layout.array_item,arr1);
        list1.setAdapter(adapter1);
        ListView list2=findViewById(R.id.list2);
        String[] arr2=new String[]{"java","hibernate","spring","android"};
        ArrayAdapter adapter2=new ArrayAdapter(this,R.layout.array_item2,arr2);
        list2.setAdapter(adapter2);
    }
}

这里特别注意arrayAdapter的三个参数:
Context:代表访问整个andriod应用的接口。几乎创建的所有组件都需要传入context中
textViewResourceld:一个资源ID。该资源代表一个textView,该textview组件将作为ArrayAdapter的列表项组件
数组或者list:该数组或list将负责为多个列表项提供数据
this,R.layout.array_item:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="24dp"
    android:padding="5dp"
    android:shadowColor="#f0f"
    android:shadowDx="4"
    android:shadowDy="4"
    android:shadowRadius="2"/>

this,R.layout.array_item2:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="24dp"
    android:padding="5dp"
    android:shadowColor="#0ff"
    android:shadowDx="4"
    android:shadowDy="4"
    android:shadowRadius="2"
   />

最终效果图


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。