Android自动建立表格
来源:互联网 发布:java 工程师要考吗 编辑:程序博客网 时间:2024/06/07 00:38
在开发中我们经常会根据需求需要自动生成一张表格,那么下面就说一下如何做。
布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_main_margin" android:paddingRight="@dimen/activity_main_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.cheng.myapplication.MainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="XX大学学生成绩单" android:textSize="30sp" android:textColor="@color/colorPrimaryDark"/> </LinearLayout> <TableLayout android:id="@+id/id_tableLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000000"> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0.5dip" android:background="#000000"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="1dip" android:background="#ffffff" android:layout_weight="1" android:gravity="center" android:text="课程" android:textSize="20dp" android:textStyle="bold"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="1dip" android:background="#ffffff" android:layout_weight="1" android:gravity="center" android:text="修习类别" android:textSize="20dp" android:textStyle="bold"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="1dip" android:background="#ffffff" android:layout_weight="1" android:gravity="center" android:text="学分" android:textSize="20dp" android:textStyle="bold"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="1dip" android:background="#ffffff" android:layout_weight="1" android:gravity="center" android:text="成绩" android:textSize="20dp" android:textStyle="bold"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="1dip" android:background="#ffffff" android:layout_weight="1" android:gravity="center" android:text="绩点" android:textSize="20dp" android:textStyle="bold"/> </TableRow> </TableLayout></LinearLayout>
资源文件/drawable/shape.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="#ffffff" android:endColor="#ffffff" android:angle="0"/> <stroke android:width="0.5dp" android:color="#dedcd2"/> <corners android:radius="2dp"/> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp"/></shape>
Java文件MainActivity.Java
package com.example.cheng.myapplication;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.ViewGroup;import android.widget.LinearLayout;import android.widget.TableLayout;import android.widget.TableRow;import android.widget.TextView;public class MainActivity extends Activity { private final int WC= ViewGroup.LayoutParams.WRAP_CONTENT; private final int FP= ViewGroup.LayoutParams.WRAP_CONTENT; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); createTable(); } public void createTable(){ TableLayout tableLayout=(TableLayout)findViewById(R.id.id_tableLayout); tableLayout.setStretchAllColumns(true); //设置指定列号的列属性为Stretchable for(int row=0;row<3;row++){ //产生的行数 TableRow tableRow=new TableRow(MainActivity.this); tableRow.setBackgroundColor(Color.rgb(222, 220, 210)); //设置表格背景 for (int col = 0; col < 5; col++) { //产生的列数 TextView tv=new TextView(MainActivity.this); tv.setBackgroundResource(R.drawable.shape); //设置背景 tv.setText("测试"); tableRow.addView(tv); } tableLayout.addView(tableRow,new LinearLayout.LayoutParams(FP, WC)); } }}
效果如图
0 0
- Android自动建立表格
- Android自动生成表格,丰富配置
- 学习android笔记(5) 动态建立TableLayout 表格布局
- CSS建立表格代码
- js动态建立表格
- Android listview 表格显示和自动循环显示
- android 通过局域网udp广播自动建立socket连接
- android 通过局域网udp广播自动建立socket连接
- android UDP多点广播,并自动建立Socket连接
- 为什么建Android工程会自动建立appcompat工程
- android 通过局域网udp广播自动建立socket连接
- android UDP多点广播,并自动建立Socket连接
- android UDP多点广播,并自动建立Socket连接
- android UDP多点广播,并自动建立Socket连接
- HTML标记【表格的建立】!
- PHP建立数据库以及表格
- TableView表格索引的建立
- 两个表格如何建立关联
- web HTML 标签
- 四皇后问题的回溯
- 2017.3.11 杭电校赛
- 用SpannableString打造绚丽多彩的文本显示效果
- pat-列车调度
- Android自动建立表格
- 83-自己实现互斥锁
- C++实验9
- Java之Collections工具类方法使用以及源码分析(二)
- 几个难记的段落CSS样式
- 简单枚举类型——植物与颜色 (sdut oj)
- web 第五节课作业-对表单的增删改
- 用jQuery Pagination插件实现的简易低效分页功能
- matplotlib绘图(极坐标 3D绘图等)(3)