gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:26:31 24 4
gpt4 key购买 nike

在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com