以下是我的代码以及 xml 文件,我在 2 个框架布局中有 2 个表格布局。第一个表格布局用于设置表格的标题,而第二个表格用于在第二个表格布局中动态设置行,尽管标题似乎非常合适,但标题下方的行没有正确对齐可能是我在做设置表格布局参数的一些错误。我正在修复中。任何帮助将不胜感激。谢谢你。
package com.table;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.YuvImage;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import java.util.List;
public class TableActivity extends Activity
{
TableLayout table,table_values;
Button scan,add;
String value;
EditText ed1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createTableLayout();
addrows();
scan = (Button)findViewById(R.id.read);
if(scan != null)
{
scan.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
}
});
}
try
{
scan.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
setContentView(R.layout.code);
add=(Button)findViewById(R.id.button2);
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
add.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void createTableLayout()
{
table = (TableLayout) findViewById(R.id.tableLayout1);
// table.setBackgroundResource(R.drawable.sky);
//TableRow tr_heading = new TableRow(this);
table_values = (TableLayout) findViewById(R.id.tableLayout2);
TableRow tr_heading = new TableRow(this);
tr_heading.setId(10);
tr_heading.setBackgroundColor(Color.GRAY);
tr_heading.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.FILL_PARENT));
TextView Serial = new TextView(this);
Serial.setId(20);
Serial.setText("Sr No. ");
Serial.setTextColor(Color.BLACK);
tr_heading.addView(Serial); // add the column to the table row
//TextView label_question = new TextView(this);
TextView Name = new TextView(this);
Name.setId(20);
Name.setText("ID ");
Name.setWidth(40);
Name.setTextColor(Color.BLACK);
tr_heading.addView(Name); // add the column to the table row
//TextView label_question = new TextView(this);
TextView Quantity = new TextView(this);
Quantity.setId(20);
Quantity.setText(" Quantity");
Quantity.setTextColor(Color.BLACK);
tr_heading.addView(Quantity); // add the column to the table row
TextView amt = new TextView(this);
amt.setId(20);
amt.setText(" Amt");
amt.setTextColor(Color.BLACK);
tr_heading.addView(amt);
table.addView(tr_heading, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.FILL_PARENT));
}
//--------------------------Adding Rows to your Table --------------------------------------
public void addrows()
{
Integer count = 0;
for (int count1 = 0; count1<3; count1++)
{
TableRow tr = new TableRow(this);
if (count % 2 != 0)
tr.setId(100 + count);
tr.setClickable(true);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
TextView sr = new TextView(this);
sr.setText("1");
//sr.setGravity(Gravity.CENTER );
sr.setPadding(2, 0, 5, 0);
sr.setTextColor(Color.WHITE);
sr.setClickable(true);
tr.addView(sr);
TextView idval = new TextView(this);
idval.setText(value);
idval.setGravity(Gravity.CENTER );
idval.setText(" fdfj");
idval.setPadding(2, 0, 5, 0);
idval.setTextColor(Color.WHITE);
idval.setClickable(true);
tr.addView(idval);
EditText quantity = new EditText(this);
//quantity.setId(200 + count);
quantity.setGravity(Gravity.CENTER );
quantity.setTextColor(Color.BLACK);
// quantity.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
// quantity.setWidth(1);
quantity.setText("1");
quantity.setInputType(InputType.TYPE_CLASS_NUMBER);
int val=Integer.parseInt((quantity.getText().toString()));
// quantity.setHeight(2);
quantity.setEnabled(true);
quantity.setPadding(2,0,5,0);
tr.addView(quantity);
TextView amount=new TextView(this);
amount.setGravity(Gravity.CENTER);
amount.setTextColor(Color.WHITE);
amount.setClickable(true);
//amount.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
amount.setEnabled(true);
//amount.setText(" 1000");
amount.setText(String.valueOf(( val*10)));
// amount.setWidth(1);
// amount.setHeight(2);
tr.addView(amount);
// finally add this to the table row
table_values.addView(tr, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
count++;
}
}
}
**Xml file is below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_height="match_parent"
android:layout_width="match_parent">
<FrameLayout
android:id="@+id/frameLayout2"
android:layout_height="match_parent"
android:layout_marginTop="40dp"
android:layout_marginBottom="70dp"
android:layout_width="match_parent">
<TableLayout
android:layout_height="wrap_content"
android:id="@+id/tableLayout1"
android:layout_width="match_parent">
</TableLayout>
<FrameLayout
android:id="@+id/frameLayout4"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_height="wrap_content" android:id="@+id/tableLayout2" android:layout_width="match_parent">
</TableLayout>
</FrameLayout>
</FrameLayout>
<FrameLayout android:id="@+id/frameLayout3" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_gravity="bottom">
<Button
android:text="Scan"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="10dp">
</Button>
<Button
android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_gravity="center_horizontal">
</Button>
</FrameLayout>
</FrameLayout>
</LinearLayout>
screen shot: http://i.stack.imgur.com/0nqsR.png
![This is ther screen image[1]
[1]: http://i.stack.imgur.com/0nqsR.png