我遵循这个 How can I create a table with borders in Android? 这非常有帮助,但在我的情况下无法创建仅显示行边框的列边框。
我的代码:
TableLayout.LayoutParams tableLayoutParams = new
TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableLayout tableLayout = new TableLayout(getContext());
tableLayout.setBackgroundColor(Color.WHITE);
tableRowParams = new TableRow.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
tableRowParams.setMargins(1, 1, 1, 1);
//tableRowParams.weight = 1;
for (i = 0; i < row + 1; i++) {
tableRow = new TableRow(getContext());
tableRow.setGravity(Gravity.CENTER_VERTICAL);
//tableRow.setBackgroundColor(Color.BLACK);
tableRow.setPadding(2, 2, 2, 2);
tableRow.setBackgroundResource(R.drawable.cell_shape_new);
for (j = 0; j < col + 1; j++) {
LinearLayout ll = new LinearLayout(getContext());
ll.setLayoutParams(new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
ll.setOrientation(1);
TextView textView = new TextView(getContext());
textView.setTextSize(25);
textView.setTextColor(Color.BLACK);
textView.setGravity(Gravity.CENTER);
//Display required field
}
}
}
R.drawable.cell_shape_new
<?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="2dp" android:color="#000000"/>
</shape>
我的输出:
如何在此处放置列边框。
最佳答案
TableRow行;
shoppingCardDetails = new ArrayList<ShoppingCardDetails>();
SoapObject courseOutlineObject = (SoapObject) ParserClass.ResponseVector
.elementAt(3);
final Vector courseOutline = (Vector) courseOutlineObject
.getProperty(0);
//System.out.println(courseOutline);
//System.out.println("check record" + courseOutline.size());
for (int current = 0; current < courseOutline.size(); current++) {
try {
row = (TableRow) LayoutInflater.from(this).inflate(
R.layout.table_row, null);
chkbox = (CheckBox) row.findViewById(R.id.chkBox);
chkbox.setId(current);
((TextView) row.findViewById(R.id.coursename))
.setText(((SoapObject) courseOutline.elementAt(current))
.getProperty("tocName").toString());
((TextView) row.findViewById(R.id.tvcourseprice))
.setText(((SoapObject) courseOutline.elementAt(current))
.getProperty("tocPrice").toString());
/*
* if ((((SoapObject) courseOutline.elementAt(current))
* .getProperty("tocLevel") + "").equalsIgnoreCase("0")) {
*
* ((LinearLayout) row.findViewById(R.id.llrow))
* .setBackgroundColor(Color.parseColor("#F2F5A9"));
*
* }
*/
if ((((SoapObject) courseOutline.elementAt(current))
.getProperty("tocEnrollStatus") + "")
.equalsIgnoreCase("true")) {
chkbox.setVisibility(View.INVISIBLE);
((TextView) row.findViewById(R.id.tvEnrolled))
.setVisibility(View.VISIBLE);
((TextView) row.findViewById(R.id.tvcourseprice))
.setVisibility(View.VISIBLE);
}
((TableLayout) findViewById(R.id.course_outline_table))
.addView(row);
chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@overide
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
try {
ShoppingCardDetails shoppingdetails = new ShoppingCardDetails();
if (isChecked) {
subTotal += 1;
total = subTotal;
shoppingdetails.name = ((SoapObject) courseOutline
.elementAt(buttonView.getId()))
.getProperty("tocName").toString();
shoppingdetails.price = ((SoapObject) courseOutline
.elementAt(buttonView.getId()))
.getProperty("tocPrice").toString();
shoppingCardDetails.add(shoppingdetails);
} else {
subTotal -= 1;
total = subTotal;
shoppingCardDetails.remove(total);
}
((TextView) findViewById(R.id.btnRightHeader))
.setText("" + total);
((TextView) findViewById(R.id.btnRightHeader))
.setPadding(30, 0, 0, 15);
((TextView) findViewById(R.id.btnRightHeader))
.setTextColor(Color.WHITE);
} catch (Exception e) {
// TODO: handle exception
}
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
关于android - 如何在android中以编程方式创建表格行和列边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21878768/