android之tableLayout自定义表格一
工作中遇到这样一个需求,要求在app端实现一个自定义table表格的模板,网上各种的实现方式有多种,但是对于tableLayout
组件的介绍以及实现,都不全面,不能实现自定义表格模板,尤其是划线部分,也让我困扰了好久,不过功夫不负有心人,
经过多次实践和努力,终于实现了利用tableLayout组件完成自定义table。
实现的效果图如下:
实现过程如下:
一 布局文件 meetingvote1.xml
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/scroll"
android:layout_gravity="top" android:background="#fdfbff"
android:layout_marginBottom="120dp">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent">
<TableLayout
android:id="@+id/myTableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp">
</TableLayout>
</LinearLayout>
</ScrollView>
二 表格划线布局
1.textview_boeder.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#b0b5b2" />
<stroke android:width="0.5dp" android:color="#000000"/>
</shape>
2. textview_border2.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<stroke android:width="0.5dip" android:color="#000000"/>
</shape>
3.textview_border3.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<stroke android:width="3dp" android:color="#000000"/>
</shape>
三 加载自定义文件,初始化table表格,部分主要代码如下:
表格头部实现:
private void initHead(TableLayout tableLayout,JSONArray userInfo){
tableLayout.setStretchAllColumns(true);
TableRow tableRow = new TableRow(this);
tableRow.setBackground(getResources().getDrawable(R.drawable.textview_border4));
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(180, ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(180,ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(320,ViewGroup.LayoutParams.MATCH_PARENT);
TextView tv = new TextView(this);
tv.setText(this.getString(R.string.level)+"\n"+this.getString(R.string.params));
tv.setTextColor(Color.parseColor("#000000"));
tv.setTextSize(20.0f);
tv.setGravity(Gravity.CENTER);
tv.setBackground(getResources().getDrawable(R.drawable.textview_border));
param.leftMargin = 3;
param.topMargin = 3;
tv.setLayoutParams(param);
LinearLayout linear = new LinearLayout(this);
linear.setOrientation(LinearLayout.HORIZONTAL);
linear.addView(tv);
TextView tv1 = new TextView(this);
tv1.setText(this.getString(R.string.level2) + "\n" + this.getString(R.string.weight));
tv1.setTextColor(Color.parseColor("#000000"));
tv1.setTextSize(20.0f);
tv1.setGravity(Gravity.CENTER);
tv1.setBackground(getResources().getDrawable(R.drawable.textview_border));
param1.topMargin = 3;
tv1.setLayoutParams(param1);
linear.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(this.getString(R.string.level_params));
tv2.setTextColor(Color.parseColor("#000000"));
tv2.setTextSize(20.0f);
tv2.setGravity(Gravity.CENTER);
tv2.setBackground(getResources().getDrawable(R.drawable.textview_border));
param2.topMargin = 3;
tv2.setLayoutParams(param2);
linear.addView(tv2);
TableRow.LayoutParams tlp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams param4 = new LinearLayout.LayoutParams(240,ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams param6 = null;
String userName="";
String levelName ="";
JSONArray levelinfo = new JSONArray();
TableLayout tableLayout2 = (TableLayout) View.inflate(getApplicationContext(), R.layout.table, null);
try{
JSONObject jsonObject = new JSONObject();
jsonObject = userInfo.getJSONObject(curr_index);
numUserID = jsonObject.getString("numUserID");
userName =jsonObject.getString("varName");
levelName =jsonObject.getString("levelName");
levelinfo = jsonObject.getJSONArray("levelinfo");
colspan = levelinfo.length();
int width = 240 * colspan;
param6 = new LinearLayout.LayoutParams(width,ViewGroup.LayoutParams.MATCH_PARENT);
}catch (Exception e){
e.printStackTrace();
}
for(int row = 0;row <3;row++){
TableRow tableRow1 = new TableRow(this);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
if(row == 2){
for(int col = 0;col<levelinfo.length();col++){
TextView tv3 = new TextView(this);
try{
JSONObject jsonObject = levelinfo.getJSONObject(col);
tv3.setText(jsonObject.getString("varName"));
}catch (Exception e){
e.printStackTrace();
}
tv3.setTextColor(Color.parseColor("#000000"));
tv3.setTextSize(20.0f);
tv3.setGravity(Gravity.CENTER);
tv3.setBackground(getResources().getDrawable(R.drawable.textview_border));
tv3.setLayoutParams(param4);
linearLayout.addView(tv3);
}
}else if(row == 0){
TextView tv3 = new TextView(this);
tv3.setText(userName);
tv3.setTextColor(Color.parseColor("#000000"));
tv3.setTextSize(20.0f);
tv3.setGravity(Gravity.CENTER);
tv3.setBackground(getResources().getDrawable(R.drawable.textview_border));
tv3.setLayoutParams(param6);
linearLayout.addView(tv3);
}else{
TextView tv3 = new TextView(this);
tv3.setText(levelName);
tv3.setTextColor(Color.parseColor("#000000"));
tv3.setTextSize(20.0f);
tv3.setGravity(Gravity.CENTER);
tv3.setBackground(getResources().getDrawable(R.drawable.textview_border));
tv3.setLayoutParams(param6);
linearLayout.addView(tv3);
}
tableRow1.addView(linearLayout);
LinearLayout.LayoutParams param8 = new LinearLayout.LayoutParams(1197, ViewGroup.LayoutParams.MATCH_PARENT);
param8.topMargin = 3;
param8.rightMargin = 3;
tableLayout2.setLayoutParams(param8);
tableLayout2.addView(tableRow1);
}
linear.addView(tableLayout2);
tableRow.addView(linear);
tableLayout.addView(tableRow);
}
这里只展示了表格头部的实现,其他部分实现请参考上述源码,这里就不再啰嗦了