3.22
所花时间:2小时
代码量:223
博客篇:1
android stdio表格的使用
编写MyTableTextView类重写onDraw方法
package com.example.studyapplication; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.widget.TextView; /** * 自定义TextView */ public class MyTableTextView extends androidx.appcompat.widget.AppCompatTextView { Paint paint = new Paint(); public MyTableTextView(Context context, AttributeSet attrs) { super(context, attrs); int color = Color.parseColor("#80b9f2"); // 为边框设置颜色 paint.setColor(color); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 画TextView的4个边 canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint); canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint); canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint); canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint); } }
主活动的xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/bt_check" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查询"/> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" android:scrollbars="none" > <HorizontalScrollView android:id="@+id/scroll_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbarAlwaysDrawHorizontalTrack="false" android:scrollbars="none"> <LinearLayout android:id="@+id/MyTable" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="9dp" android:orientation="vertical" > </LinearLayout> </HorizontalScrollView> </ScrollView> </LinearLayout>
主活动:
package com.example.studyapplication; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.RelativeLayout; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import java.sql.SQLException; public class recordStats extends AppCompatActivity implements View.OnClickListener { private LinearLayout mainLinerLayout; private Button bt_check; private RelativeLayout relativeLayout; private Record[] r=new Record[100]; private String[] name={"序号","学号","姓名","班级","开始时间","结束时间","学习记录"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_record_stats); bt_check = findViewById(R.id.bt_check); for(int i=0;i<100;i++){ r[i]=new Record(); } mainLinerLayout = (LinearLayout) this.findViewById(R.id.MyTable); initData(); bt_check.setOnClickListener(this); } //绑定数据 private void initData() { //初始化标题 relativeLayout = (RelativeLayout) LayoutInflater.from(recordStats.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); new Thread(new Runnable() { @Override public void run() { Dao d =new Dao(); try { r=d.checkRecord(); } catch (SQLException e) { throw new RuntimeException(e); } } }).start(); //初始化内容 } @Override public void onClick(View v) { Log.d("www", r[0].getId()); int number = 1; int i=0; while (r[i].getId()!=null){ relativeLayout = (RelativeLayout) LayoutInflater.from(recordStats.this).inflate(R.layout.table, null); MyTableTextView txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_1); txt.setText(String.valueOf(number)); txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_2); txt.setText(r[i].getId()); txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_3); txt.setText(r[i].getName()); txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_4); txt.setText(r[i].getUnit()); txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_5); txt.setText(r[i].getStarttime()); txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_6); txt.setText(r[i].getEndtime()); txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_7); txt.setText(r[i].getRecord()); Log.d("www", "onClick: "); mainLinerLayout.addView(relativeLayout); number++; i++; } } }