android TableLayout根本没有出现

DIN*_*LIT 1 android tablelayout

我希望一旦我点击按钮,就会出现包含该表格的片段.除表格布局外,一切都会显示.我没弄明白为什么.

这是我的表格布局:

<android.support.v4.widget.NestedScrollView
        android:id="@+id/nested_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        android:scrollingCache="true">
        <TableLayout android:id="@+id/simpleTableLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            xmlns:android="http://schemas.android.com/apk/res/android" >
            <TableRow
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:gravity="center_horizontal">
                <TextView
                    android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:textSize="18dp" android:text="Row 1"  android:layout_span="3"
                    android:padding="18dip" android:background="#b0b0b0"
                    android:textColor="#000"/>
                <TextView
                    android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:textSize="18dp" android:text="Row 1"  android:layout_span="3"
                    android:padding="18dip" android:background="#b0b0b0"
                    android:textColor="#000"/>
            </TableRow>
        </TableLayout>
    </android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

这是我片段的重要方法:

class TablePresentation : DialogFragment(),tableContract.View
{
...
override fun onCreateView(inflater: LayoutInflater?, parent: ViewGroup?, state: Bundle?): View? {
    super.onCreateView(inflater, parent, state)
    val view = activity.layoutInflater.inflate(R.layout.table_fragment, parent, false)
    view.findViewById<View>(R.id.button_close)?.setOnClickListener({ dismiss() })
    return view
}

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    table = view?.findViewById<View>(R.id.simpleTableLayout) as TableLayout
    row = LayoutInflater.from(context).inflate(R.layout.attrib_row, null) as TableRow
    attName= (row?.findViewById<View>(R.id.attrib_name) as TextView)
    attVal=(row?.findViewById<View>(R.id.attrib_value) as TextView)
}


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(DialogFragment.STYLE_NORMAL, R.style.FullScreenDialogStyle)
}
...
}
Run Code Online (Sandbox Code Playgroud)

在考虑到第一个答案后,表格会出现,但是一旦我尝试使用此代码动态地执行此操作:

class TablePresentation : DialogFragment(),tableContract.View
{
...
    var table: TableLayout?=null
    var row:TableRow?=null
    var attName:TextView?=null
    var attVal:TextView?=null

...
    override fun updateTable(temps: ArrayList<Double>) {
        for (i in 0 until temps.size) {
            attName?.setText((i+1).toString())
            attVal?.setText(temps.get(i).toString())
            table?.addView(row)
        }
        table?.requestLayout()
    }
}
Run Code Online (Sandbox Code Playgroud)

它在第一次点击时不会显示任何结果.

and in the second clik it crashes and shows: 
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.tharwa.tdm2_exo2, PID: 10114
                  java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                      at android.view.ViewGroup.addViewInner(ViewGroup.java:4454)
                      at android.view.ViewGroup.addView(ViewGroup.java:4295)
                      at android.widget.TableLayout.addView(TableLayout.java:426)
                      at android.view.ViewGroup.addView(ViewGroup.java:4235)
                      at android.widget.TableLayout.addView(TableLayout.java:408)
                      at android.view.ViewGroup.addView(ViewGroup.java:4208)
                      at android.widget.TableLayout.addView(TableLayout.java:399)
                      at com.tharwa.tdm2_exo2.TableAgent.TablePresentation.updateTable(TablePresentation.kt:63)
...
Run Code Online (Sandbox Code Playgroud)

我还没有理解如何首先打电话removeView()给孩子的父母,以及如何让它显示出来.

DIN*_*LIT 5

但是为了最小化代码行并使我们可以使用更多的awsom:这是表视图

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/nested_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    android:scrollingCache="true">
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/simpleTableLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="45dp"
        android:layout_marginRight="45dp"
        android:stretchColumns="*"
        >
    </TableLayout>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

这里使用"attrib_row.xml"的行

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/border"
    >
    <TextView
        android:id="@+id/attrib_name"
        android:textStyle="bold"
        android:height="30dp"
        android:background="@drawable/border"
        android:gravity="center"
        />
    <TextView
        android:id="@+id/attrib_value"
        android:gravity="center"
        android:height="30dp"
        android:textStyle="bold"
        android:background="@drawable/border"
        />
</TableRow>
Run Code Online (Sandbox Code Playgroud)

我们可以将这个xml文件添加到drawable中,以便为我们的表"border.xml"添加边框

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape= "rectangle">
    <solid android:color="@color/colorAccent"/>
    <stroke android:width="1dp" android:color="#000000"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

最后这里是紧凑的代码

fun updateTable()
{
    val temps=controller?.getTemps()
    val rowHead = LayoutInflater.from(context).inflate(R.layout.attrib_row, null) as TableRow
    (rowHead.findViewById<View>(R.id.attrib_name) as TextView).text=("time")
    (rowHead.findViewById<View>(R.id.attrib_value) as TextView).text=("Value")
    table!!.addView(rowHead)
    for (i in 0 until temps!!.size) {

        val row = LayoutInflater.from(context).inflate(R.layout.attrib_row, null) as TableRow
        (row.findViewById<View>(R.id.attrib_name) as TextView).text=((i+1).toString())
        (row.findViewById<View>(R.id.attrib_value) as TextView).text=(temps[i].toString())
        table!!.addView(row)
    }
    table!!.requestLayout()
}
Run Code Online (Sandbox Code Playgroud)