Android 简单表格列表 安卓实现表格

您所在的位置:网站首页 如何在excel中实现冻结首行的效果 Android 简单表格列表 安卓实现表格

Android 简单表格列表 安卓实现表格

2023-07-12 04:43| 来源: 网络整理| 查看: 265

Android 自定义TextView控件,用来组成表格方便数据的展示。

首先看一下效果

Android 简单表格列表 安卓实现表格_xml

样式不是很好看,需要用的可以自己优化一下。

实现方式很简单。

  1、自定义控件 MyTableTextView 继承 TextView 重写onDraw方法。在里面添加话边框的操作。

1 package lyf.com.mytableview; 2 3 import android.content.Context; 4 import android.graphics.Canvas; 5 import android.graphics.Color; 6 import android.graphics.Paint; 7 import android.util.AttributeSet; 8 import android.widget.TextView; 9 10 /** 11 * lyf on 2016/06/27 12 * 自定义TextView 13 */ 14 public class MyTableTextView extends TextView { 15 16 Paint paint = new Paint(); 17 18 public MyTableTextView(Context context, AttributeSet attrs) { 19 super(context, attrs); 20 int color = Color.parseColor("#80b9f2"); 21 // 为边框设置颜色 22 paint.setColor(color); 23 } 24 25 @Override 26 protected void onDraw(Canvas canvas) { 27 super.onDraw(canvas); 28 // 画TextView的4个边 29 canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint); 30 canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint); 31 canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint); 32 canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint); 33 } 34 }

View Code

  2、主布局,什么也不用写。直接放一个LinearLayout就好。但因为表格的宽度和高度往往超过屏幕的宽高度,因此需要我们添加ScrollView 和HorizontalScrollView。为了让HorizontalScrollView填满整个ScrollView 需要在ScrollView设置属性 android:fillViewport="true"。

View Code

  3、接下来写表格显示的样式文件table.xml。该布局文件用来显示表格每一行的样式。

View Code

  4、最后把数据放到我们的table.xml文件中,并显示到我们的主布局文件中。

package lyf.com.mytableview; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.LinearLayout; import android.widget.RelativeLayout; /** * lyf on 2016/06/27 * 自定义表格显示 */ public class MainActivity extends Activity { private LinearLayout mainLinerLayout; private RelativeLayout relativeLayout; private String[] name={"序号","考号","姓名","出生年月","语文","数学","英语"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mainLinerLayout = (LinearLayout) this.findViewById(R.id.MyTable); initData(); } //绑定数据 private void initData() { //初始化标题 relativeLayout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.table, null); MyTableTextView title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_1); title.setText(name[0]); title.setTextColor(Color.BLUE); title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_2); title.setText(name[1]); title.setTextColor(Color.BLUE); title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_3); title.setText(name[2]); title.setTextColor(Color.BLUE); title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_4); title.setText(name[3]); title.setTextColor(Color.BLUE); title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_5); title.setText(name[4]); title.setTextColor(Color.BLUE); title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_6); title.setText(name[5]); title.setTextColor(Color.BLUE); title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_7); title.setText(name[6]); title.setTextColor(Color.BLUE); mainLinerLayout.addView(relativeLayout); //初始化内容 int number = 1; for (int i=0;i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3