java - android TableLayout 添加行动态只添加最后一行

标签 java android user-interface tablelayout tablerow

在 StackOverflow 上阅读了数十个线程之后。 我正在尝试向表格布局动态添加行。 问题是,只有一行是动态添加的,而且它跨越了整个页面。 这是我的代码:

package com.example.trashproject;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;

public class MainActivity extends FragmentActivity{


    private static final int ROWS =2;
    private static final int COLS = 10;
    private static final String TAG = null;
    private TableLayout mTable;
    private View[][] mCircles;
    private boolean[][] mData;

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

    }

    private void makeTable() {
        mTable = (TableLayout) findViewById(R.id.table);

        TableRow.LayoutParams rowParams = new TableRow.LayoutParams();
        rowParams.width = LayoutParams.WRAP_CONTENT;
        rowParams.height = LayoutParams.WRAP_CONTENT;
        mCircles = new View[ROWS][COLS];
        TableRow row;
        for (int i = 0; i < ROWS; i++) {
            row = new TableRow(this);
            row.setLayoutParams(rowParams);
            for (int j = 0; j < COLS; j++) {
                mCircles[i][j] = new View(this);
                mCircles[i][j].setBackgroundResource(R.drawable.small_circle);
                row.addView(mCircles[i][j]);
            }
            mTable.addView(row);
        }
    }

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/table"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    <TableRow >
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TEXT"/>
    </TableRow>
    <TableRow >
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TEXT"/>
    </TableRow>
    </TableLayout>

</LinearLayout>

calendar_month_grid.xml:

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="1dp"
    />

small_circle.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <size android:width="15dp"
        android:height="1dp"/>
    <solid android:color="@android:color/holo_green_light"/>

</shape>

感谢您的帮助...

最佳答案

我认为是“ View ”给您带来了问题。尝试用 ImageView 替换它(尝试一下),我认为它会起作用。如果您真的想要查看(不确定为什么),请将最小高度和最小宽度设置为 10dp 之类的值。

我曾经遇到过,带有环绕内容的空 View 跨越整个页面。它将您的其余行推出屏幕

关于java - android TableLayout 添加行动态只添加最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27864030/

相关文章:

java - CRC32 - 更改初始值

android - AsyncTask 和 Handlers 有什么区别,在 Listview 中使用哪个更好?

java - JTable 作为 MVC 框架的一部分

python - python GUI的问题

java - 根据用户字符串检查 IE 是否为 11,并从代理字符串中获取版本

java - 如何在 Spring WebServiceTemplate 中设置超时

java - 如何等待List中的所有Monos完成(成功与否无关紧要)

android - 无法为 retrofit2.Response<...> 创建调用适配器

java - com.google.gson.Gson 类因 javax.crypto.SecretKey 而失败?

java - 方法创建多个面板