Android开发之动态生成表格及其边框

修改布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".execl.ExeclVie"
    android:orientation="vertical">


    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="动态生成表格和边框"/>
    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:stretchColumns="*"
        android:background="#DEDCD2">
        <TableRow
            android:background="#DEDCD2"
            android:layout_margin="0.5dip">
            <TextView
                android:text="年度"
                android:textSize="20dip"
                android:textStyle="bold"
                android:background="#FFFFFF"/>
            <TextView
                android:text="本金"
                android:textSize="20dip"
                android:textStyle="bold"
                android:background="#FFFFFF"/>
            <TextView
                android:text="利息"
                android:textSize="20dip"
                android:textStyle="bold"
                android:background="#FFFFFF"/>
        </TableRow>

    </TableLayout>
    <TableLayout
        android:id="@+id/TableLayout01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:stretchColumns="*"
        android:background="#DEDCD2">

        <TableRow
            android:background="#DEDCD2"
            android:layout_margin="0.5dip">

            <TextView
                android:text=""
                android:textSize="20dip"
                android:textStyle="bold"
                android:background="#FFFFFF"
                android:layout_margin="1dip" />
            <TextView
                android:text=""
                android:textSize="20dip"
                android:textStyle="bold"
                android:background="#FFFFFF"
                android:layout_margin="1dip" />



            <TextView
                android:text=""
                android:textSize="20dip"
                android:textStyle="bold"
                android:background="#FFFFFF"
                android:layout_margin="1dip" />










        </TableRow>





    </TableLayout>

</LinearLayout>

添加资源文件

<?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>

修改主文件

package com.example.mpcc.mqttandroid.execl;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.example.mpcc.mqttandroid.R;

public class ExeclVie extends AppCompatActivity {

    private final int wc = ViewGroup.LayoutParams.WRAP_CONTENT;
    private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
    Button myButton;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_execl_vie);
        myButton = (Button)findViewById(R.id.myButton);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TableLayout tableLayout = (TableLayout)findViewById(R.id.TableLayout01);
                tableLayout.setStretchAllColumns(true);
                for(int row=0;row<6;row++){
                    TableRow tableRow = new TableRow(ExeclVie.this);
                    tableLayout.setBackgroundColor(Color.rgb(222,220,210));
                    for(int col = 0;col<3;col++){
                        TextView tv = new TextView(ExeclVie.this);
                        tv.setBackgroundResource(R.drawable.shapee);
                        tv.setText("选择");
                        tableRow.addView(tv);


                    }
                    tableLayout.addView(tableRow,new TableLayout.LayoutParams(FP,wc));

                }
            }
        });

    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值