动态生成表格布局
package com.anyikang.emergency120.aek.activity;
import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.anyikang.emergency120.aek.R;
public class RescueOrderNotifyActivity extends AppCompatActivity {
LinearLayout layoutPush;
private Drawable drawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout tableLayout = new TableLayout(this);
drawable = this.getResources().getDrawable(R.drawable.table_border);
tableLayout.setDividerDrawable(drawable);
tableLayout.setShowDividers(TableLayout.SHOW_DIVIDER_BEGINNING|TableLayout.SHOW_DIVIDER_END|TableLayout.SHOW_DIVIDER_MIDDLE);
tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
for(int i = 0; i < 3; i++)
{
TableRow tableRow = new TableRow(this);
tableRow.setOrientation(LinearLayout.HORIZONTAL);
tableRow.setDividerDrawable(drawable);
tableRow.setShowDividers(TableLayout.SHOW_DIVIDER_BEGINNING|TableLayout.SHOW_DIVIDER_END|TableLayout.SHOW_DIVIDER_MIDDLE);
TextView textView = new TextView(this);
TextView textView2 = new TextView(this);
textView.setText("标题"+i);
textView2.setText("标题"+(i+1));
textView.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1));
textView2.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1));
tableRow.addView(textView);
tableRow.addView(textView2);
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
}
}
在drawable文件夹下创建XML shape资源文件:table_border
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="1dp"
android:height="1dp" />
<solid android:color="@color/main_bg" />
</shape>
其中的color自己定义吧,在此不再贴出。