教你制作表格样式的ListView
来源:互联网 发布:用户积分数据库设计 编辑:程序博客网 时间:2024/06/07 03:59
最近在做工作流的项目,项目开发中需要把项目的明细用表格的样式展示在ListView控件上,前前后后测试了几个demo运用到项目中,感觉相当佩服他们,所以也尝试着学习他们自己制作了一个demo,就当自娱自乐,仅供参考。你们也可以参考以下博客:
http://blog.csdn.net/bewhatyouare/article/details/8373584
http://www.diannao.wang/anzhuo/2015/52-81030.html
项目中由于单条信息过长,一个屏幕不可能完全展示出来,所以用到了HorizontalScrollView控件,此控件可以实现屏幕的横向滚动。
项目开发中ListView适配器的布局界面list_item.xml如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" android:orientation="horizontal" > <TextView android:id="@+id/name" style="@style/itemStyle" android:layout_width="140dp" /> <View style="@style/viewStyle" android:visibility="visible" /> <TextView android:id="@+id/age" style="@style/itemStyle" android:layout_width="70dp" /> <View style="@style/viewStyle" android:visibility="visible" /> <TextView android:id="@+id/height" style="@style/itemStyle" android:layout_width="55dp" /> <View style="@style/viewStyle" android:visibility="visible" /> <TextView android:id="@+id/weight" style="@style/itemStyle" android:layout_width="150dp" /> <View style="@style/viewStyle" android:visibility="visible" /> <TextView android:id="@+id/school" style="@style/itemStyle" android:layout_width="150dp" /></LinearLayout>
Activity用到的布局界面activity_main.xml如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="40dip" android:background="#fc8510" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="标题" android:textColor="#ffffff" android:textSize="18sp" /> </RelativeLayout> <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="2dip" android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/list" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView style="@style/itemHeaderStyle" android:layout_width="140dp" android:text="@string/name" /> <View style="@style/viewStyle" android:visibility="visible" /> <TextView style="@style/itemHeaderStyle" android:layout_width="70dp" android:text="@string/age" /> <View style="@style/viewStyle" android:visibility="visible" /> <TextView style="@style/itemHeaderStyle" android:layout_width="55dp" android:text="@string/height" /> <View style="@style/viewStyle" android:visibility="visible" /> <TextView style="@style/itemHeaderStyle" android:layout_width="150dp" android:text="@string/weight" /> <View style="@style/viewStyle" android:visibility="visible" /> <TextView style="@style/itemHeaderStyle" android:layout_width="150dp" android:text="@string/school" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:background="#848484" android:visibility="visible" /> <ListView android:id="@id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="@null" android:divider="#848484" android:dividerHeight="1px" android:scrollbarSize="10.0dip" > </ListView> <View android:layout_width="fill_parent" android:layout_height="1px" android:background="#848484" android:visibility="visible" /> </LinearLayout> </HorizontalScrollView></LinearLayout>
为了增强代码的规范性,降低代码的耦合性,使用的资源设定在适当的地方。
上面的布局界面中使用到的样式在res/values/styles.xml文件中:
<resources> <!-- 列表表头样式 --> <style name="itemHeaderStyle"> <item name="android:background">#c8c8c8</item> <item name="android:gravity">center</item> <item name="android:textColor">#3C3C3C</item> <item name="android:textSize">15sp</item> <item name="android:layout_height">25dp</item> </style> <!-- 横线样式 --> <style name="viewStyle"> <item name="android:layout_width">1px</item> <item name="android:layout_height">fill_parent</item> <item name="android:background">#848484</item> </style> <!-- 列表样式 --> <style name="itemStyle"> <item name="android:gravity">center</item> <item name="android:textColor">#3C3C3C</item> <item name="android:textSize">15sp</item> <item name="android:singleLine">true</item> <item name="android:layout_height">25dp</item> </style></resources>
布局界面中使用到的字符串资源在res/values/strings.xml文件中:布局界面中使用到的字符串资源应
<string name="name">姓名</string> <string name="age">年龄</string> <string name="height">身高</string> <string name="weight">体重</string> <string name="school">学校</string>
布局界面中使用到的图片、背景资源放在res/background文件夹中:<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- 定义边框内部颜色 --> <solid android:color="#FFFFFF" /> <!-- 定义边框颜色和宽度 --> <stroke android:width="1.0px" android:color="#3C3C3C" /> <!-- 定义边框内部开始和结束时的颜色 --> <gradient android:endColor="#FFFFFF" android:startColor="#FFFFFF" /> <!-- 定义内边距 --> <padding android:bottom="1.0px" android:left="1.0px" android:right="1.0px" android:top="1.0px" /></shape>接下来就该我们的适配器上场了:我们自定义适配器让它继承自BaseAdapter:public class StudentAdapter extends BaseAdapter {private LayoutInflater inflater;private List<Student> students;private Context context = null;public ReportAdapter(List<Student> students, Context context) {super();this.students= students;this.context = context;this.inflater = LayoutInflater.from(context);}public void setStudents(List<Student> students) {this.students = students;}@Overridepublic int getCount() {return students.size();}@Overridepublic Object getItem(int position) {return students.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder;if(convertView == null){convertView = inflater.inflate(R.layout.list_item, null);holder = new ViewHolder();holder.name = (TextView) convertView.findViewById(R.id.name);holder.age = (TextView) convertView.findViewById(R.id.age);holder.height = (TextView) convertView.findViewById(R.id.height);holder.weight = (TextView) convertView.findViewById(R.id.weight);holder.school = (TextView) convertView.findViewById(R.id.school); convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}int[] colors = { Color.WHITE, Color.parseColor("#d5d5d5") };convertView.setBackgroundColor(colors[position % 2]);Student student = students.get(position);holder.name.setText(student.getName());holder.age.setText(student.getAge());holder.height.setText(student.getHeight());holder.weight.setText(student.getWeight());holder.school.setText(student.getSchool());return convertView;}private class ViewHolder{TextView name;TextView age;TextView height;TextView weight;TextView school;}}public class MainActivity extends ListActivity {private List<Student>> list;private StudentAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);list = new ArrayList<Student>>();//Student实体类我没有创建构造方法,系统也会自动创建了一个无参的构造方法,里面的属性可有可无Student s1 = new Student(); s1.setName("张三"); s1.setAge(19);Student s2 = new Student(); s2.setName("李四"); s2.setHeight(175);Student s3 = new Student(); s3.setName("王五"); s3.setWeight(65);list.add(s1);list.add(s2);list.add(s3);adapter = new ListAdapter(MainActivity.this, list);getListView().setAdapter(adapter);}}
1 0
- 教你制作表格样式的ListView
- listView做成的表格样式
- 使用ListView制作表格样式以及消息提示小图标的布局
- 使用ListView制作表格样式以及消息提示小图标的布局
- listView做成的表格样式二
- VB制作迅雷样式的Listview
- 教你如何制作精美的圆角表格
- android listview实现表格样式
- android listview实现表格样式 .
- ListView实现类似课程表,出诊表的表格样式
- 论文制作自动嵌套表格格式和样式的使用
- Android 利用ListView制作带竖线的多彩表格
- Android 利用ListView制作带竖线的多彩表格
- Android 利用ListView制作带竖线的多彩表格
- Android 利用ListView制作带竖线的多彩表格
- Android 利用ListView制作带竖线的多彩表格
- Android 利用ListView制作带竖线的多彩表格
- Android 利用ListView制作带竖线的多彩表格
- 打包python 加icon 去掉cmd黑窗口
- Spring框架
- js 学习笔记 (一)
- PHP学习笔记——boolean布尔类型;
- 最大连续数列的和
- 教你制作表格样式的ListView
- 激光雷达知名供应商 你知道几家?
- Windows平台下安装Eclipse插件,开发Hadoop应用
- gcc的__builtin_函数介绍
- POJ1002
- iOS 开发之多线程GCD介绍
- (第1篇)什么是hadoop大数据?我又为什么要写这篇文章?
- 机器学习: TensorFlow 的数据读取与TFRecords 格式
- 信安实验-RC4加密算法