以编程方式将TableRows添加到TableLayout不显示

时间:2016-07-04 12:26:11

标签: android android-tablelayout

我是Android新手。我尝试将TableRows添加到TableLayout,但它不起作用。我不知道为什么它没有显示。我试图从互联网上看,但似乎我的问题不一样。我在logcat中没有异常或错误。我试图调试并发现添加了视图但由于某种原因它们没有显示。有没有人对此有所了解?提前致谢。 我的代码:

private void addInfosToTable(){ //this is called in onCreate() function
//these are just dummy infos i want to test the how the table works
    Cell cell = new Cell("1234",5000,3000);
    cell.setStatus(Cell.Status.COMPLETE);
    Cell cell1 = new Cell("1234",5000,3000);
    cell1.setStatus(Cell.Status.FAILED);
    addCellRowToTable(cell);
    addCellRowToTable(cell1);
}

    private void addCellRowToTable(Cell cell){
    //init cell info
    String cellID = cell.getCellID();
    double targetSpeed = cell.targetSpeed();
    double targetPoint = cell.targetPoint();
    Cell.Status status = cell.getStatus();

    //create tableRow
    TableRow tableRow = new TableRow(this);
    tableRow.addView(getTextView(cellID,true));
    tableRow.addView(getTextView("n.a"+"/ "+"n.a",false));
    tableRow.addView(getTextView(targetSpeed+"/ "+targetPoint,false));
    tableRow.addView(getStatusView(status));
     tableRow.setPadding(0,R.dimen.table_marginVertical,R.dimen.table_marginVertical,0);
    tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT));

    //add tableRow to tableLayout
    this.cellTable.addView(tableRow);

}
private TextView getTextView(String text, boolean isFirst){
    TextView textView = new TextView(this);
    textView.setTextSize(R.dimen.table_textSize);
    textView.setText(text);
    textView.setTextColor(ContextCompat.getColor(this,R.color.black));
    if (isFirst){
        textView.setPadding(R.dimen.table_marginVertical,0,0,0);
    }
    return textView;

}
private ImageView getStatusView(Cell.Status status){
    ImageView imageView = new ImageView(this);
    switch (status){
        case COMPLETE:
            imageView.setImageResource(R.drawable.success);
            return imageView;
        default:
            imageView.setImageResource(R.drawable.failed);
            return imageView;
    }
}

我的表有4列,看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="superapp.networkapp.MainActivity"
android:focusableInTouchMode="true">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.google.android.gms.maps.MapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="false"
    android:layout_alignParentEnd="false">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/warning_box"
        android:padding="10dp"
        android:layout_margin="3dp"
        android:weightSum="1">

        <TextView
            android:layout_width="276dp"
            android:layout_height="wrap_content"
            android:text="@string/warning_text"
            android:id="@+id/main_boxText"
            android:layout_margin="3dp"
            android:textColor="@color/black"
            android:textStyle="bold"
            android:layout_weight="0.49"
            android:textSize="12sp"
            android:textIsSelectable="true" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/main_boxIcon"
            android:src="@drawable/warning_icon"
            android:layout_weight="0.47" />
    </LinearLayout>

    <TableLayout
        android:stretchColumns = "*"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/cellListTable">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/blue"
            android:paddingTop="@dimen/table_marginVertical"
            android:paddingBottom="@dimen/table_marginVertical"
            android:id="@+id/cellTableHeading">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/cells"
                android:id="@+id/cell"
                android:layout_column="0"
                android:textColor="@color/white"
                android:textColorHighlight="@color/blue"
                android:textSize="@dimen/table_textSize"
                android:textIsSelectable="true"
                android:paddingLeft="@dimen/table_marginVertical" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/table_current_info"
                android:id="@+id/currentInfo"
                android:layout_column="1"
                android:textColor="@color/white"
                android:textColorHighlight="@color/blue"
                android:textSize="@dimen/table_textSize"
                android:textIsSelectable="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/table_target_info"
                android:id="@+id/targetInfo"
                android:layout_column="2"
                android:textColor="@color/white"
                android:textColorHighlight="@color/blue"
                android:textSize="@dimen/table_textSize"
                android:textIsSelectable="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/status"
                android:id="@+id/status"
                android:layout_column="3"
                android:textColor="@color/white"
                android:textColorHighlight="@color/blue"
                android:textSize="@dimen/table_textSize"
                android:textIsSelectable="true"
                android:textAlignment="textEnd"
                android:paddingRight="@dimen/table_marginVertical" />
        </TableRow>
    </TableLayout>
</LinearLayout>

我不知道它为什么没有显示。我试图从互联网上看,但似乎我的问题不一样。我在logcat中没有异常或错误。我试图调试并发现添加了视图但由于某种原因它们没有显示。有没有人对此有所了解?提前谢谢。

更新 我将默认视图的布局高度编辑为所有wrap_content。我还添加了这行代码:在将tableRow添加到tableLayout之前。但它仍然无效。

tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT));

1 个答案:

答案 0 :(得分:0)

您必须为每个表行定义布局,并将textViews和ImageView绑定到布局中的相应元素。