我创建了一个表格布局并动态添加行和列,但是当我添加更多数据时,它无法正常显示。
我的输出页面看起来像这样(但这不是我想要的格式,因为它没有在一个屏幕上显示):
这是我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_table"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/rltrackorder"
android:fillViewport="true">
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TableLayout
android:id="@+id/table_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true">
</TableLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
这是我的java代码:
package shababpost.receiver;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class Activity_Home_TrackOrder extends Activity {
TableLayout table_parent;
RelativeLayout relativeLayouthome;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_home_track_order);
init();
}
public void init() {
relativeLayouthome = (RelativeLayout) findViewById(R.id.rltrackorder);
relativeLayouthome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent trackordr = new Intent(Activity_Home_TrackOrder.this, Activity_TrackOrder.class);
startActivity(trackordr);
}
});
TableLayout table_parent = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
tbrow0.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
// tbrow0.setGravity(Gravity.CENTER);
tbrow0.setPadding(0, 30, 0, 30);
tbrow0.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView tv0 = new TextView(this);
tv0.setText(" SR.No ");
tv0.setTextColor(Color.BLACK);
tbrow0.setGravity(Gravity.CENTER);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Order Id ");
tbrow0.setGravity(Gravity.CENTER);
tv1.setTextColor(Color.BLACK);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Date ");
tv2.setTextColor(Color.BLACK);
tbrow0.setGravity(Gravity.CENTER);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" Amount ");
tv3.setTextColor(Color.BLACK);
tbrow0.setGravity(Gravity.CENTER);
tbrow0.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setText(" Status ");
tv4.setTextColor(Color.BLACK);
tbrow0.setGravity(Gravity.CENTER);
tbrow0.addView(tv4);
TextView tv5 = new TextView(this);
tv5.setText(" View ");
tv5.setTextColor(Color.BLACK);
tbrow0.setGravity(Gravity.CENTER);
tbrow0.addView(tv5);
table_parent.addView(tbrow0);
for (int i = 0; i < 15; i++) {
TableRow tbrow = new TableRow(this);
tbrow.setGravity(Gravity.CENTER);
tbrow.setPadding(0, 15, 0, 15);
TextView t1v = new TextView(this);
t1v.setText("1" + i);
t1v.setSingleLine(true);
t1v.setTextColor(Color.BLACK);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
TextView t2v = new TextView(this);
t2v.setText("#ASAS456" + i);
t2v.setTextColor(Color.BLACK);
t2v.setSingleLine(true);
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);
TextView t3v = new TextView(this);
t3v.setText("09-March-17" + i);
t3v.setTextColor(Color.BLACK);
t3v.setGravity(Gravity.CENTER);
t3v.setSingleLine(true);
tbrow.addView(t3v);
TextView t4v = new TextView(this);
t4v.setText("205 AED" + i);
t4v.setTextColor(Color.BLACK);
t4v.setGravity(Gravity.CENTER);
t4v.setSingleLine(true);
tbrow.addView(t4v);
TextView t5v = new TextView(this);
t5v.setText("pending" + i);
t5v.setTextColor(Color.BLACK);
t5v.setGravity(Gravity.CENTER);
t5v.setSingleLine(true);
tbrow.addView(t5v);
TextView t6v = new TextView(this);
t6v.setText("View" + "");
t6v.setTextColor(Color.WHITE);
t6v.setSingleLine(true);
t6v.setGravity(Gravity.CENTER);
t6v.setBackgroundResource(R.drawable.continue_btn_style);
tbrow.addView(t6v);
table_parent.addView(tbrow);
}
}
}