在ListView头和尾添加东西
直接上代码
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView lv; private List<String>list; private ArrayAdapter<String>adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 lv=(ListView)findViewById(R.id.lv); //初始化数据 list=new ArrayList<String>(); for (int i=0;i<20;i++){ list.add("显示内容为"+i); } //初始化适配器 adapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,list); //把xml文件转化为View对象 View image=LayoutInflater.from(this).inflate(R.layout.image_item,null); //把图片放到ListView的头 lv.addHeaderView(image); Button btn=new Button(this); btn.setText("加载更多"); //把按钮放在ListView的尾部 lv.addFooterView(btn); //设置适配器 lv.setAdapter(adapter); } }
新学到的是用到是 addHeaderView addFooterView两个方法
还有两个xml文件
主活动
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.example.aaaaa.myapplication.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv"> </ListView> </RelativeLayout>
设置图片XML文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="200dp"> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:src="@drawable/jj" android:scaleType="fitXY"/> </LinearLayout>
相关推荐
83580494 2013-07-19
langjiao 2013-07-16
kiduo0 2013-07-10
gongzhiyao0 2010-11-15
bigdatazx 2010-11-05
Urchindong 2011-08-01
peixiaopao 2011-08-21
MeOrdinary 2014-05-13
magic00 2019-10-21
guizhongyun 2011-09-27
csuhanshuai 2015-03-30
Sunanang 2015-03-30
snailbing 2015-04-23
huohu00 2015-04-22
toperfect 2015-07-01
nickey 2012-01-29
Rgenxiao 2012-01-26
满城风絮 2011-12-03
nickey 2011-12-16