2016-03-04 77 views
1

我正在創建一個應用程序,我需要在它內部有動態表,所以我試圖這樣做,而且大多數情況下,它的工作原理。但我有我的行和列之間的這些分隔線,我用視圖來這樣做,他們出來這樣:Android:在TableLayout中的視圖以編程方式添加不顯示

編輯:我認爲意見只是沒有顯示,我的第一列是左邊界應該是,不知道這是幹什麼。

Dividers appear in the title row

這是我用來生成行的代碼:

private void generateMaterialRows(){ 
     TableLayout tableMaterials = (TableLayout) findViewById(R.id.info_table_materials); 
      //create counter for index in table 
      int c = 3; 
     for(Material m: materials){ 


      //makes Rows 
      TableRow tRow = new TableRow(this); 
      TableRow tDivider = new TableRow(this); 

      //Makes 5 bottomBorders and puts them in an Array 
      View[] bottomBorders = new View[5]; 

      for (int i = 0; i < 5; i++) { 
       View bottomBorder = new View(this); 
       bottomBorder.setBackgroundResource(R.color.colorDarkGray); 
       int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics()); //converts dp to px 
       bottomBorder.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height)); 
       bottomBorder.setLayoutParams(bottomBorder.getLayoutParams()); 

       bottomBorders[i] = bottomBorder; 
      } 

      //makes 3 sideBorders and puts them in an Array 
      View[] sideBorders = new View[3]; 

      for (int i = 0; i < 3; i++){ 
       View sideBorder = new View(this); 
       sideBorder.setBackgroundResource(R.color.colorDarkGray); 
       int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics()); //converts dp to px 
       sideBorder.setLayoutParams(new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT)); 
       sideBorder.setLayoutParams(sideBorder.getLayoutParams()); 

       sideBorders[i] = sideBorder; 
      } 

      //makes columnAmount 
      TextView txtAmount = new TextView(this); 
      txtAmount.setBackgroundResource(R.color.colorBlueLight); 
      txtAmount.setPadding(5,5,5,5); 
      txtAmount.setText(String.valueOf(m.getAmount())); 

      //makes columnName 
      TextView txtName = new TextView(this); 
      txtName.setBackgroundResource(R.color.colorBlueLight); 
      txtName.setPadding(5, 5, 5, 5); 
      txtName.setText(String.valueOf(m.getName())); 

      //puts content in TableRow 
      tRow.addView(sideBorders[0]); 
      tRow.addView(txtAmount); 
      tRow.addView(sideBorders[1]); 
      tRow.addView(txtName); 
      tRow.addView(sideBorders[2]); 

      //makes divider 
      for (int i = 0; i < 5; i++){ 
       tDivider.addView(bottomBorders[i]); 
      } 

      tableMaterials.addView(tRow, c); 
      tableMaterials.addView(tDivider, c+1); 

      c++; 
     } 
    } 

這是TableLayout中的XML:

   <TableLayout 
        android:id="@+id/info_table_materials" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="@dimen/activity_vertical_margin"> 
        <!-- Divider Above Title Row --> 
        <TableRow> 
         <View 
          android:background="@color/colorDarkGray" 
          android:layout_height="3px"/> 
         <View 
          android:background="@color/colorDarkGray" 
          android:layout_height="3px"/> 
         <View 
          android:background="@color/colorDarkGray" 
          android:layout_height="3px"/> 
         <View 
          android:background="@color/colorDarkGray" 
          android:layout_height="3px"/> 
         <View 
          android:background="@color/colorDarkGray" 
          android:layout_height="3px"/> 
        </TableRow> 

        <!-- Title Row --> 
        <TableRow> 
         <View 
          android:background="@color/colorDarkGray" 
          android:layout_width="3px" 
          android:layout_height="fill_parent"/> 
         <TextView 
          android:text="@string/info_amount" 
          android:background="@color/colorLightGray" 
          android:paddingRight="10dp" 
          android:paddingLeft="10dp" 
          android:paddingTop="5dp" 
          android:paddingBottom="5dp"/> 
         <View 
          android:background="@color/colorDarkGray" 
          android:layout_width="3px" 
          android:layout_height="fill_parent"/> 
         <TextView 
          android:text="@string/info_materials" 
          android:background="@color/colorLightGray" 
          android:paddingRight="10dp" 
          android:paddingLeft="10dp" 
          android:paddingTop="5dp" 
          android:paddingBottom="5dp"/> 
         <View 
          android:background="@color/colorDarkGray" 
          android:layout_width="3px" 
          android:layout_height="fill_parent"/> 
        </TableRow> 

        <!-- Divider Between Title and Content Rows --> 
        <TableRow> 
         <View 
          android:background="@color/colorDarkGray" 
          android:layout_height="3px"/> 
         <View 
          android:background="@color/colorDarkGray" 
          android:layout_height="3px"/> 
         <View 
          android:background="@color/colorDarkGray" 
          android:layout_height="3px"/> 
         <View 
          android:background="@color/colorDarkGray" 
          android:layout_height="3px"/> 
         <View 
          android:background="@color/colorDarkGray" 
          android:layout_height="3px"/> 
        </TableRow> 

       </TableLayout> 

我還在安卓相當新所以我仍然在學習,但這個讓我感到困惑,我似乎無法找到問題所在。

回答

1

我發現了這個問題。我不知道如何使用LayoutParams,所以我使用ViewGroup.LayoutParams但由於他們在TableRow我應該使用TableRow.LayoutParams

相關問題