布局中含有ListView的几点注意
来源:互联网 发布:centos 破解root密码 编辑:程序博客网 时间:2024/06/04 18:10
一、主类
package com.example.listviewdemo;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends Activity {private ListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (ListView) this.findViewById(R.id.listview);ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getData());listView.setAdapter(adapter);}private ArrayList<String> getData() {ArrayList<String> data = new ArrayList<String>();data.add("item1");data.add("item2");data.add("item3");data.add("item4");data.add("item5");data.add("item6");data.add("item7");data.add("item8");data.add("item9");data.add("item10");return data;}}
二、布局1
场景
布局文件
<ScrollView 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" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" /> <ListView android:id="@+id/listview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:divider="@android:drawable/divider_horizontal_textfield" /> </LinearLayout></ScrollView>三、布局2
场景
布局文件
<LinearLayout 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" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" /> <ListView android:id="@+id/listview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:divider="@android:drawable/divider_horizontal_textfield" /></LinearLayout>这种场景适用于做标题栏
布局文件
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#BEFEBE" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/arrow_drawer" /> </LinearLayout> <ListView android:id="@+id/listview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:divider="@android:drawable/divider_horizontal_textfield" /></LinearLayout>头部和尾部标题栏
布局文件
<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" android:orientation="vertical" tools:context=".MainActivity" > <LinearLayout android:id="@+id/layout01" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:background="#BEFEBE" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/arrow_drawer" /> </LinearLayout> <LinearLayout android:id="@+id/layout02" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#BEFEBE" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/arrow_drawer" /> </LinearLayout> <ListView android:id="@+id/listview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/layout02" android:layout_below="@id/layout01" android:divider="@android:drawable/divider_horizontal_textfield" /></RelativeLayout>
四、布局3
场景
从图片中看出有2个item1,其实第一个item1是addHeaderView中的ListView中的第一条,由于listView.addHeaderView(view);但是因为这个view必须是一个固定大小的一个View才行,由于ListView含有很多View,所以只显示第一条.
主类
package com.example.listviewdemo;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends Activity {private ListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (ListView) this.findViewById(R.id.listview);View view = LayoutInflater.from(this).inflate(R.layout.headerview01, null);listView.addHeaderView(view);//addHeaderView方法必须在setAdapter之前调用ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getData());listView.setAdapter(adapter);ListView header_list = (ListView) view.findViewById(R.id.header_listView);header_list.setAdapter(adapter);}private ArrayList<String> getData() {ArrayList<String> data = new ArrayList<String>();data.add("item1");data.add("item2");data.add("item3");data.add("item4");data.add("item5");data.add("item6");data.add("item7");data.add("item8");data.add("item9");data.add("item10");return data;}}
activity_main.xml
<LinearLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity" > <ListView android:id="@+id/listview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:divider="@android:drawable/divider_horizontal_textfield" /></LinearLayout>
headerview01.xml
<?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" android:orientation="vertical" > <ListView android:id="@+id/header_listView" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ListView></LinearLayout>
//这里,即使将LinearLayout的高设置成固定的数值,结果还是一样,只显示第一条。
如果想要在ListView布局之上的东西或者之下的东西可以和ListView一起滚动,可以参考这种方案,但是注意HeadView和FooterView中不能含有ListView,结果如下:
0 0
- 布局中含有ListView的几点注意
- ListView中含有多个item布局的处理
- ListView条目混乱需要注意的几点内容
- php中需要注意的几点
- ListView嵌套ListView,listVIew中含有各种不同布局的item
- ListView中嵌套EditText,直接在EditText输入,需要注意几点
- 项目的实现中所需要注意的几点
- array的几点注意
- 索引的几点注意
- ACL的几点注意
- 注意virtual的几点!
- iocp注意的几点
- 搜索引擎优化中选择关键字需要注意的几点
- C语言中“命名”需要注意的几点
- 浅谈Hibernate中HQL查询需要注意的几点
- Delphi中使用Lua脚本语言的几点注意!
- C语言中表达式求值要注意的几点
- Java程序员考试中需要注意的几点
- QTableView表格视图的列宽设置
- Android中低电量的处置方法
- VS2010制作安装包
- PowerDesigner 生成带注释SQL 各个版本通用10(12、15)
- [Android]ListView焦点陷阱
- 布局中含有ListView的几点注意
- 涂料小知识
- VMware安装centos6.5,共享文件夹
- dlms/cosem研究
- Xcode修改IOS应用程序名称
- COCOS2D-X 快速入门基础知识总结1
- poj3557 Map Generator(概率dp)及其扩展
- [图]小米内部培训PPT曝光:这待遇真不错
- Bootstrap 轮播