[英]Android: TableLayout doesn't show
I'm new to Android app programming. 我是Android应用程序编程的新手。
I'm trying to use a Table Layout to list some notes. 我正在尝试使用表格布局列出一些注释。 Because their number shouldn't be limited, I have to integrate it programically.
因为不应该限制它们的数量,所以我必须以编程方式对其进行集成。
I created a new project for test purposes: 我出于测试目的创建了一个新项目:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
public class MainActivity extends AppCompatActivity {
private TableLayout table;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater inflater = (LayoutInflater) getSystemService(MainActivity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_main, null);
table = view.findViewById(R.id.table);
table.setOrientation(TableLayout.HORIZONTAL);
TableRow tr = new TableRow(MainActivity.this);
TableRow.LayoutParams paramrow = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
tr.setLayoutParams(paramrow);
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
tr.addView(b);
table.addView(tr, TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
}
}
My XML: 我的XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.chrobin.beung.MainActivity">
<TableLayout
android:id="@+id/table"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableLayout>
</android.support.constraint.ConstraintLayout>
It isn't a duplicate because all answers by now involved using TableRow.LayoutParams instead os LayoutParams. 它不是重复的,因为到目前为止所有答案都涉及使用TableRow.LayoutParams而不是os LayoutParams。
Thanks! 谢谢!
Change your XML file with this below code , you was using 0dp
as height
and width
of your table
so it was not working , i have corrected your code - 使用以下代码更改您的XML文件,您使用
0dp
作为your table
height
和width
, 因此它无法正常工作 ,我已更正了您的代码-
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.chrobin.beung.MainActivity">
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableLayout>
</android.support.constraint.ConstraintLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.