简体   繁体   中英

Table to take entire screen in android

在此处输入图片说明 I want my table to take the entire screen with spacing between columns and rows. I am creating my rows dynamically in java depending upon the number of players. Where in java code I add the properties so that the table take the entire screen. But I could not achieve it. Attached below is my code and xml file.

<LinearLayout 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:orientation="vertical"
    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="views.controllers.RoundInfo">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="addRound"
        android:text="@string/addround" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:text="@string/roundinfo"
        android:textSize="30sp" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none">

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TableLayout
                android:id="@+id/maintable"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </HorizontalScrollView>

    </ScrollView>


</LinearLayout>


    public void addData() {
            numOfPlayers = currentRound.getPlayers().size();
            row = new String[numOfPlayers];
            for (int i = 0; i < numOfPlayers; i++) {
                row[i] = currentRound.getPlayers().get(i).getPlayer().getName();
            }
            col = new String[]{"Winner", "Seen", "Less", "Points"}; // get from database

            int rowCount = row.length;

            for (int i = 0; i <= rowCount; i++) {
                TableRow tableRow = new TableRow(this);
                TableLayout.LayoutParams tableRowParams=
                        new TableLayout.LayoutParams
                                (TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT);

                int leftMargin=10;
                int topMargin=2;
                int rightMargin=10;
                int bottomMargin=2;

                tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);

                tableRow.setLayoutParams(tableRowParams);
                // create tableRow
                for (int j = 0; j <= 4; j++) {
                    //create textView

                    TextView textView = new TextView(this);
                    textView.setGravity(Gravity.CENTER);
                    CheckBox checkBox = new CheckBox(this);
                    EditText point = new EditText(this);
                    if (i == 0 && j == 0) {
                        textView.setText(" ");
                        tableRow.addView(textView);
                    } else if (i > 0 && j == 0) {
                        textView.setText(row[i - 1]); // Player Header
                        tableRow.addView(textView);
                    } else if (i == 0 && j != 0) {
                        textView.setText(col[j - 1]); //Game Header
                        tableRow.addView(textView);
                    } else if (i > 0 && j == 1) {
                        checkBox.setText(""); // Is Winner
                        tableRow.addView(checkBox);
                    } else if (i > 0 && j == 2) {
                        checkBox.setText(""); // Is Seen
                        tableRow.addView(checkBox);
                    } else if (i > 0 && j == 3) {
                        checkBox.setText(""); // Is Less
                        tableRow.addView(checkBox);
                    } else if (i > 0 && j == 4) {
                        point.setInputType(100); // Points
                        tableRow.addView(point);
                    }
                }
                tl.addView(tableRow);
            }
        }

if you want your table to take as much space as possible on the screen, remove margin and padding from it's parents

for example how the root layout has padding, remove this

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

This can be made a lot easier, if you reconsider how you are implementing your table.

Have empty TableLayout in your layout with space you want it to take in UI,

Have a separate layout for a TableRow which would be populated in the TableLayout , you will use this separate layout for row with LayoutInflator and populate as many rows you need

 LayoutInflater layoutInflater = LayoutInflater.from(getApplicationContext());
 layoutInflater.inflate(R.layout.your_single_row, parent, false);

this will return a View object which you can add to the TableLayout

tableLayout.addView("you view will come here which you get from the inflater");

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