简体   繁体   中英

Add Dynamic TableView in a Horizontal ScrollView

I am trying to find out how can somebody make a TableView without specifing it in an XML file. I have two activities:

  1. Takes the number of rows and columns
  2. Makes a TableView with the given rows and columns (the cells are filled with random numbers between 0 - 2)

So, this one workes fine, but when i'm trying to put too many columns, obviously the numbers are barely seen.

The second activity:

    public class TableCreator extends Activity {

    private String getRandomPoints(){
        Random rn = new Random();

        return new Integer(Math.abs(rn.nextInt()%3)).toString();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table_creator);

        Bundle extras = getIntent().getExtras();
        int rows = extras.getInt("rows");
        int columns = extras.getInt("columns");


        TableLayout table = (TableLayout) findViewById(R.id.TableLayout);
        for(int i=0;i<rows;i++){
            TableRow row = new TableRow(this);
            row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

            for(int j=0;j<columns;j++){
                TextView tv = new TextView(this);
                tv.setText(getRandomPoints());
                tv.setLayoutParams(new TableRow.LayoutParams(0,LayoutParams.WRAP_CONTENT,1));
                tv.setGravity(Gravity.CENTER);

                row.addView(tv);
            }
            table.addView(row);
        }
    }
}

And the XML File:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableLayout"
    android:stretchColumns="0,1"
    android:layout_weight="1"    
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</TableLayout> 

After that I tried to add a HorizontalScrollView in the xml file, so the TableView to be inside of it. This one was raising an exception, because the system couldn't decide the excact width an height that the TableLayout would use...

I also tried to add a HorizontalScrollView programmatically, but the numbers didn't show up. I can say that I searched carefully in this site about same questions, but still the answers are a little bit irrelevant to mine...

Here ya go you can set the Layout as a horizontal scroll view and the tablelayout is the child

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".EvacRouteTableActivity" >

<TableLayout android:id="@+id/TableLayout01" android:layout_width="match_parent" android:layout_height="wrap_content"  android:stretchColumns="0">

  <TableRow 
      android:id="@+id/TableRow01" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content">
    <TextView 
        android:id="@+id/TextView01" 
        android:background="@drawable/cell_shape"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text=" Evacuation Routes (To view route click name)"
        android:width="340dp"
        android:height="30dp"
        android:textStyle="bold"></TextView>

  </TableRow>

</TableLayout>  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM