package org.woa.view;
import org.woa.activity.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class TableView extends TableLayout {
private int mColNum, mRowNum;
private boolean mHeader;
private int mTableHeight, mTableWidth;
private int mCellBackgroundResource;
private Drawable mCellBackgroundDrawable;
private TableRow[] trList;
private View[][] cellList;
public TableView(Context context, AttributeSet attrs) {
super(context, attrs);
this.bindAttributes(context, attrs);
// setup the table view
this.trList = new TableRow[this.mRowNum];
this.cellList = new View[this.mRowNum][this.mColNum];
View cell;
TableRow tr;
char A = 'A';
for (int row = 0; row < this.mRowNum; row++) {
tr = trList[row] = new TableRow(context);
for (int col = 0; col < this.mColNum; col++) {
if (mHeader && row == 0 && col != 0) {
cell = new TextView(context);
((TextView) cell).setText(A++ + "");
((TextView) cell).setTextColor(Color.BLACK);
} else if (mHeader && col == 0 && row != 0) {
cell = new TextView(context);
((TextView) cell).setText(row + "");
((TextView) cell).setTextColor(Color.BLACK);
} else {
cell = new View(context);
}
cellList[row][col] = cell;
cell.setId(row * 10 + col);
if (mCellBackgroundResource == -1) {
cell.setBackgroundDrawable(mCellBackgroundDrawable);
} else {
cell.setBackgroundResource(this.mCellBackgroundResource);
}
if (cell instanceof TextView) {
((TextView) cell).setGravity(Gravity.CENTER);
((TextView) cell).setTextSize(20);
}
TableRow.LayoutParams lp_cell = new TableRow.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
lp_cell.weight = 1;
cell.setLayoutParams(lp_cell);
tr.addView(cell, col);
}
TableLayout.LayoutParams lp_tr = new TableLayout.LayoutParams();
lp_tr.weight = 1;
this.addView(tr, lp_tr);
}
}
public void bindAttributes(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.TableView);
this.mHeader = a.getBoolean(R.styleable.TableView_mHeader, false);
this.mCellBackgroundResource = a.getInt(
R.styleable.TableView_mCellBackgroundResource, -1);
this.mCellBackgroundDrawable = a
.getDrawable(R.styleable.TableView_mCellBackgroundDrawable);
this.mColNum = a.getInt(R.styleable.TableView_mColNum, 4);
this.mRowNum = a.getInt(R.styleable.TableView_mRowNum, 4);
if (mHeader) {
this.mColNum++;
this.mRowNum++;
}
}
public int getTableHeight() {
return mTableHeight;
}
public void setTableHeight(int tableHeight) {
this.mTableHeight = tableHeight;
}
public int getTableWidth() {
return mTableWidth;
}
public void setTableWidth(int tableWidth) {
this.mTableWidth = tableWidth;
}
public int getTableColNum() {
return mColNum;
}
public void setTableColNum(int tableColNum) {
this.mColNum = tableColNum;
}
public int getTableRowNum() {
return mRowNum;
}
public void setTableRowNum(int tableRowNum) {
this.mRowNum = tableRowNum;
}
}
attribute:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TableView">
<attr format="string" name="mColNum" />
<attr format="string" name="mRowNum" />
<attr format="string" name="mHeader" />
<attr format="string" name="mCellBackgroundResource" />
<attr format="string" name="mCellBackgroundDrawable" />
</declare-styleable>
</resources>
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fsms="http://schemas.android.com/apk/res/org.woa.activity"
android:orientation="horizontal" android:baselineAligned="true"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<org.woa.view.TableView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/playground"
fsms:mRowNum="6" fsms:mColNum="6" fsms:mHeader="true"
fsms:mCellBackgroundDrawable="@android:drawable/btn_default"
android:background="@android:drawable/toast_frame">
</org.woa.view.TableView>
</LinearLayout>