【问题标题】:How can I create a table with borders in Android?如何在 Android 中创建带边框的表格?
【发布时间】:2010-01-21 10:35:26
【问题描述】:

我使用表格布局将数据显示为表格,但我想要一个带有用户定义的列和行的表格,并带有边框。有什么建议吗?

【问题讨论】:

    标签: android tablelayout


    【解决方案1】:

    我对这个问题的解决方案是在每个单元格的背景字段上放置一个 xml 可绘制资源。通过这种方式,您可以为所有单元格定义一个带有所需边框的形状。唯一的不便是极端单元格的边框宽度只有其他单元格的一半,但如果你的表格填满整个屏幕也没问题。

    一个例子:

    drawable/cell_shape.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape
      xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape= "rectangle"  >
            <solid android:color="#000"/>
            <stroke android:width="1dp"  android:color="#ff9"/>
    </shape>
    

    布局/my_table.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TableRow
            android:id="@+id/tabla_cabecera"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></TableRow>
    
        <TableLayout
            android:id="@+id/tabla_cuerpo"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cell_shape"
                    android:padding="5dp"
                    android:text="TextView"
                    android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cell_shape"
                    android:padding="5dp"
                    android:text="TextView"
                    android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cell_shape"
                    android:padding="5dp"
                    android:text="TextView"
                    android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
    
            </TableRow>
    
            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cell_shape"
                    android:padding="5dp"
                    android:text="TextView"
                    android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cell_shape"
                    android:padding="5dp"
                    android:text="TextView"
                    android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cell_shape"
                    android:padding="5dp"
                    android:text="TextView"
                    android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
            </TableRow>
    
            <TableRow
                android:id="@+id/tableRow3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cell_shape"
                    android:padding="5dp"
                    android:text="TextView"
                    android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cell_shape"
                    android:padding="5dp"
                    android:text="TextView"
                    android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cell_shape"
                    android:padding="5dp"
                    android:text="TextView"
                    android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
    
            </TableRow>
    
            <TableRow
                android:id="@+id/tableRow4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cell_shape"
                    android:padding="5dp"
                    android:text="TextView"
                    android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cell_shape"
                    android:padding="5dp"
                    android:text="TextView"
                    android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cell_shape"
                    android:padding="5dp"
                    android:text="TextView"
                    android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
    
            </TableRow>
        </TableLayout>
    
    
    </LinearLayout>
    

    编辑:一个例子

    Edit2:另一个示例(具有更多元素:圆角、渐变...)

    我已经在http://blog.intelligenia.com/2012/02/programacion-movil-en-android.html#more 中详细解释了这个问题。它是西班牙语,但有一些更复杂表格的代码和图像。

    【讨论】:

    • 我无法查看内容
    • 如何以编程方式设置视图的背景? view.setBackground(?)
    • 为了查看这个,只需删除名为 tabla_cabecera 的第一行
    • 这将使边框看起来至少有 2px 厚,这不如 1px 宽的边框。
    • 是的,正如我所解释的,这是唯一的不便之处,但如果您的表格填满整个屏幕,您可以将底部和左侧边框设置为 1px(例如),您将获得一个边框为 1px 的表格。
    【解决方案2】:

    我必须同意布拉德的观点。那是一个糟糕的答案。 Android 文档指出 TableLayout 容器不显示边框线,因此将它们发送到 Android 站点不会对它们有一点帮助。我能够在 droidnova 上找到一个“肮脏”的解决方案,其中包括为 TableLayout 设置背景颜色,然后为 TableRow 设置不同的背景颜色并将 layout_margin 添加到行中。我不喜欢这个解决方案,但它确实适用于行边界。我想你可以对组成每个“单元”项目的项目做同样的事情,但我还没有验证。

    一个类似于 DroidNova 的例子:

    <TableLayout android:background="#000000"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
      <TableRow android:background="#FFFFFF"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_margin="1dp">
         ...
      </TableRow>
    </TableLayout>
    

    【讨论】:

    • 小心这里故意透支的问题,如果表格很大,可能会导致 UI 卡顿/卡顿。
    • 如何以编程方式在 TableRow 对象上设置 layout_margin?
    • 它只是在表格及其行周围添加轮廓,而不是在列之间添加轮廓
    【解决方案3】:

    如果您只是想在行之间有一条线(例如,就在“总计”行上方),那么有一个简单的解决方案 - 只需添加具有背景颜色和特定 layout_height 的 TableRow,例如:

    <TableRow android:layout_height="1px" android:background="#BDBDBD">
       <TextView android:layout_span="2" android:layout_height="1px" 
                 android:layout_width="fill_parent" android:text="">
       </TextView>
    </TableRow>
    

    设置android:layout_height="1px" 或者你想要的边框有多厚。填写尽可能多的空 TextView 列以匹配表的其余部分,或者像我演示的那样仅使用一个与 android:layout_span 一起使用。

    输出将如下所示:

    如果您尝试添加更复杂的边框,那么已经发布的其他答案更合适。

    【讨论】:

    • 当您可以使用View 时,为什么要使用TextView?此外,指定确切的像素也不是一个好习惯。请改用 dp/sp。另请参阅此线程:stackoverflow.com/questions/2025282/…
    • 感谢您的反馈...我想没有理由使用 TextView - 这只是一个示例。其次,除非我误解了 dp/sp(很可能,实际上,因为我只在 Android 上开发了大约一周)我使用 px 因为我只想要我的行之间有保证的 1 像素线,无论如何我正在使用的分辨率或屏幕尺寸。我喜欢很细的线条。其他用户可能有不同的口味并使用其他单位。
    • 是的,但你喜欢很细的线条吗? - 说好的简单解决方案得到了我的投票
    【解决方案4】:

    我想要的是这样一张桌子

    我在我的 styles.xml 中添加了这个:

          <style name="Divider">
            <item name="android:layout_width">1dip</item>
            <item name="android:layout_height">match_parent</item>
            <item name="android:background">@color/divider_color</item>
        </style>
    
        <style name="Divider_invisible">
            <item name="android:layout_width">1dip</item>
            <item name="android:layout_height">match_parent</item>
        </style>
    

    然后在我的表格布局中:

     <TableLayout
                android:id="@+id/table"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:stretchColumns="*" >
    
                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:background="#92C94A" >
    
                    <TextView
                        android:id="@+id/textView11"
                        android:paddingBottom="10dp"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:paddingTop="10dp" />
    
                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent" >
    
                        <View style="@style/Divider_invisible" />
                    </LinearLayout>
    
                    <TextView
                        android:id="@+id/textView12"
                        android:paddingBottom="10dp"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:paddingTop="10dp"
                        android:text="@string/main_wo_colon"
                        android:textColor="@color/white"
                        android:textSize="16sp" />
    
                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent" >
    
                        <View style="@style/Divider" />
                    </LinearLayout>
    
                    <TextView
                        android:id="@+id/textView13"
                        android:paddingBottom="10dp"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:paddingTop="10dp"
                        android:text="@string/side_wo_colon"
                        android:textColor="@color/white"
                        android:textSize="16sp" />
    
                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent" >
    
                        <View style="@style/Divider" />
                    </LinearLayout>
    
                    <TextView
                        android:id="@+id/textView14"
                        android:paddingBottom="10dp"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:paddingTop="10dp"
                        android:text="@string/total"
                        android:textColor="@color/white"
                        android:textSize="16sp" />
                </TableRow>
    
                <!-- display this button in 3rd column via layout_column(zero based) -->
    
                <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#6F9C33" >
    
                    <TextView
                        android:id="@+id/textView21"
                        android:padding="5dp"
                        android:text="@string/servings"
                        android:textColor="@color/white"
                        android:textSize="16sp" />
    
                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent" >
    
                        <View style="@style/Divider" />
                    </LinearLayout>
    
    ..........
    .......
    ......
    

    【讨论】:

      【解决方案5】:

      您也可以通过程序来执行此操作,而不是通过 xml,但它有点“hackish”。但是给一个男人没有选择,你让他别无选择:p..这是代码:

      TableLayout table = new TableLayout(this);
      TableRow tr = new TableRow(this);
      tr.setBackgroundColor(Color.BLACK);
      tr.setPadding(0, 0, 0, 2); //Border between rows
      
      TableRow.LayoutParams llp = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
      llp.setMargins(0, 0, 2, 0);//2px right-margin
      
      //New Cell
      LinearLayout cell = new LinearLayout(this);
      cell.setBackgroundColor(Color.WHITE);
      cell.setLayoutParams(llp);//2px border on the right for the cell
      
      
      TextView tv = new TextView(this);
      tv.setText("Some Text");
      tv.setPadding(0, 0, 4, 3);
      
      cell.addView(tv);
      tr.addView(cell);
      //add as many cells you want to a row, using the same approach
      
      table.addView(tr);
      

      【讨论】:

        【解决方案6】:

        要在每个单元格周围制作 1dp 折叠边框,而无需编写 java 代码,也无需使用&lt;shape...&gt; 标签创建另一个 xml 布局,您可以尝试以下解决方案:

        &lt;TableLayout...&gt; 中添加 android:background="#CCC"android:paddingTop="1dp"android:stretchColumns="0"

        &lt;TableRow...&gt; 中添加 android:background="#CCC"android:paddingBottom="1dp"android:paddingRight="1dp"

        在 TableRow 中的每个单元格/子项中,即 &lt;TextView...&gt; 添加 android:background="#FFF"android:layout_marginLeft="1dp"

        按照所述遵循填充和边距非常重要。此解决方案将在 (X)HTML/CSS 中绘制一个 1dp 边框,即边框折叠属性。

        &lt;TableLayout...&gt;&lt;TableRow...&gt; 中的背景颜色表示边框线颜色,&lt;TextView...&gt; 中的背景填充表格单元格。如有必要,您可以在单元格中添加一些填充。

        这里有一个例子:

        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#CCC"
            android:paddingTop="1dp"
            android:stretchColumns="0"
            android:id="@+id/tlTable01">
        
            <TableRow
                android:background="#CCC"
                android:paddingBottom="1dp"
                android:paddingRight="1dp">
                <TextView 
                    android:layout_marginLeft="1dp"
                    android:padding="5dp"
                    android:background="#FFF"
                    android:text="Item1"/>
                <TextView 
                    android:layout_marginLeft="1dp"
                    android:padding="5dp"
                    android:background="#FFF"
                    android:gravity="right"
                    android:text="123456"/>
            </TableRow>
            <TableRow
                android:background="#CCC"
                android:paddingBottom="1dp"
                android:paddingRight="1dp">
                <TextView 
                    android:layout_marginLeft="1dp"
                    android:padding="5dp"
                    android:background="#FFF"
                    android:text="Item2"/>
                <TextView 
                    android:layout_marginLeft="1dp"
                    android:padding="5dp"
                    android:background="#FFF"
                    android:gravity="right"
                    android:text="456789"/>
            </TableRow>
        </TableLayout>
        

        【讨论】:

        • 太棒了!谢谢!
        • 这很好,但是当你想使用暗模式时会出现问题。因为您必须放入 TableRow background = white 才能有白色边框。现在,当您的第二个 TextView 比第一个 TextView 更长(更高)时,您将在第一个 TextView 后面有白色背景。将第一个 TextView 的背景设置为深色将无济于事,因为它不会覆盖整个视图。
        • 这就是我要找的...谢谢@WiktorKalinowski 所说的也适用于我们必须手动玩的黑暗模式。
        【解决方案7】:

        在这里,我通过以下设计图像设计了列表。我的列表项文件名为 Propertylistitem.xml 并且 cellborder.xml 用于单元格边框输出的可绘制形状,显示在此图像中。我在这里添加了必要的代码。

        文件名:propertylistitem.xml

        <TableLayout... >
                    <TableRow... >
                         <TextView ...
                            android:background="@drawable/cellborder"
                            android:text="Amount"/>
                    </TableRow>
        
                    <TableRow... >
                        <TextView...
                            android:background="@drawable/cellborder"
                            android:text="5000"/>
                    </TableRow>
                </TableLayout>
        

        文件名:cellborder.xml 在这里,我只想要我的设计中的边框,所以我把评论放在纯色标签上。

        <?xml version="1.0" encoding="utf-8"?>
            <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
            <!--     <solid android:color="#dc6888"/>     -->
                <stroke android:width="0.1dp" android:color="#ffffff"
                    />
                <padding android:left="0dp" android:top="0dp"
                        android:right="0dp" android:bottom="0dp" />
            </shape>
        

        【讨论】:

          【解决方案8】:

          经过长时间的搜索和数小时的尝试,这是我可以编写的最简单的代码:

          ShapeDrawable border = new ShapeDrawable(new RectShape());
          border.getPaint().setStyle(Style.STROKE);
          border.getPaint().setColor(Color.BLACK);
          tv.setBackground(border);
          content.addView(tv);
          

          tv 是一个带有简单文本的 TextView,内容是我的容器(在本例中为 LinearLayout)。 这样容易一些。

          【讨论】:

          • 需要 API 16 级 :(
          • setBackgroundDrawable() 可以代替。
          【解决方案9】:

          嗯,这可能会激励你 这些步骤展示了如何动态创建带边框的表格

          这是表格视图

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

          这里是要使用的行"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>
          

          我们可以将这个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>
          

          最后是用 Kotlin 编写的紧凑代码,但如果需要,可以很容易地将其转换为 java

          temps 是一个包含数据的数组列表:ArrayList&lt;Double&gt;()

          fun CreateTable()
          {
              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()
          }
          

          你可以在你的 fragment 中使用它,例如像这样

             override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
                  super.onViewCreated(view, savedInstanceState)
                  table = view?.findViewById<View>(R.id.simpleTableLayout) as TableLayout
                  CreateTable()
              }
          

          最终的结果是这样的

          【讨论】:

            【解决方案10】:

            重写 onDraw 方法然后在画布上画线怎么样?

            for(int i = 0; i < rows; i++)
                {
                    canvas.drawLine(0, i * m_cellHeight, m_totalWidth, i * m_cellHeight, paint);
                }
                for(int i = 0; i < m_columns; i++){
                    canvas.drawLine(i* m_cellWidth, 0, i * m_cellWidth, m_cellHeight * rows, paint);
                }
            

            【讨论】:

              【解决方案11】:

              我使用了这个解决方案:在TableRow 中,我为每个单元格LinearLayout 创建了带有垂直线和实际单元格的单元格,并且在每个TableRow 之后添加了一条水平线。

              看下面的代码:

              <TableLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:shrinkColumns="1">
              
                  <TableRow            
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" >
              
                          <LinearLayout 
                              android:orientation="horizontal"
                              android:layout_height="match_parent"
                              android:layout_weight="1">
              
                              <TextView 
                                  android:layout_width="0dp"
                                  android:layout_height="wrap_content"
                                  android:gravity="center"/>
              
                          </LinearLayout>
              
                          <LinearLayout 
                              android:orientation="horizontal"
                              android:layout_height="match_parent"
                              android:layout_weight="1">
              
                              <View
                                  android:layout_height="match_parent"
                                  android:layout_width="1dp"
                                  android:background="#BDCAD2"/>
              
                              <TextView 
                                  android:layout_width="0dp"
                                  android:layout_height="wrap_content"
                                  android:gravity="center"/>
              
                          </LinearLayout>
                    </TableRow>
              
                    <View
                      android:layout_height="1dip"
                      android:background="#BDCAD2" />
              
                    <!-- More TableRows -->
              </TableLayout>
              

              希望它会有所帮助。

              【讨论】:

              • 这是一种在行下方添加单行的简单方法。
              【解决方案12】:

              这是解决这个问题的好方法:

              创建一个带有圆角的可绘制矩形,如下所示:

              <?xml version="1.0" encoding="utf-8"?>
              <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
                  <stroke android:width="2dp"
                          android:color="#888888"/> 
              
                  <corners android:bottomRightRadius="6dp" 
                           android:bottomLeftRadius="6dp" 
                           android:topLeftRadius="6dp" 
                           android:topRightRadius="6dp"/> 
              </shape>
              

              将其保存在名为rounded_border.xml的drawable文件夹中

              然后创建一个使用 rounded_border 作为背景的相对布局,如下所示:

              <?xml version="1.0" encoding="utf-8"?>
              <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" 
                  android:background="@drawable/rounded_border">
                 <ListView 
                     android:id="@+id/list_view"
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"/>
              
              </RelativeLayout>
              

              将其保存在布局文件夹中并将其命名为 table_with_border.xml

              然后,当您需要这样的表时,使用如下的包含语法将其拉入视图:

              <include
                      android:id="@+id/rounded_table"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      layout="@layout/table_with_border" />
              

              您可能希望在边缘周围添加一些间距 - 因此只需将包含包含在 LinearLayout 中并在边缘周围添加一些填充。

              在桌子周围设置漂亮边框的简单方法。

              【讨论】:

                【解决方案13】:

                如果您需要带边框的表格,我建议使用带有权重的线性布局而不是 TableLayout。

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:gravity="center"
                    android:padding="7dp"
                    android:background="@drawable/border"
                    android:textColor="@android:color/white"
                    android:text="PRODUCT"/>
                
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:background="@android:color/black"
                    android:paddingStart="1dp"
                    android:paddingEnd="1dp"
                    android:paddingBottom="1dp"
                    android:baselineAligned="false">
                
                    <LinearLayout
                        android:layout_weight="1"
                        android:layout_height="fill_parent"
                        android:layout_width="0dp">
                
                        <TextView
                            android:id="@+id/chainprod"
                            android:textSize="15sp"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@android:color/white"
                            android:gravity="center"
                            android:textColor="@android:color/black"
                            android:text="@string/pdct"/>
                
                    </LinearLayout>
                
                    <LinearLayout
                        android:layout_weight="1"
                        android:layout_height="fill_parent"
                        android:layout_width="0dp"
                        android:layout_marginStart="1dp">
                
                        <TextView
                            android:id="@+id/chainthick"
                            android:textSize="15sp"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@android:color/white"
                            android:gravity="center"
                            android:textColor="@android:color/black"
                            android:text="@string/thcns"/>
                
                    </LinearLayout>
                
                    <LinearLayout
                        android:layout_weight="1"
                        android:layout_height="fill_parent"
                        android:layout_width="0dp"
                        android:layout_marginStart="1dp">
                
                        <TextView
                            android:id="@+id/chainsize"
                            android:textSize="15sp"
                            android:layout_width="fill_parent"
                            android:layout_height="40dp"
                            android:background="@android:color/white"
                            android:gravity="center"
                            android:textColor="@android:color/black"
                            android:text="@string/size" />
                    </LinearLayout>
                
                    <LinearLayout
                        android:layout_weight="1"
                        android:layout_height="fill_parent"
                        android:layout_width="0dp"
                        android:layout_marginStart="1dp">
                
                        <TextView
                            android:textSize="15sp"
                            android:layout_width="fill_parent"
                            android:layout_height="40dp"
                            android:background="@android:color/white"
                            android:gravity="center"
                            android:textColor="@android:color/black"
                            android:text="@string/sqft" />
                    </LinearLayout>
                
                </LinearLayout>
                
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:background="@android:color/black"
                    android:paddingStart="1dp"
                    android:paddingEnd="1dp"
                    android:paddingBottom="1dp"
                    android:baselineAligned="false">
                
                    <LinearLayout
                        android:layout_weight="1"
                        android:layout_height="fill_parent"
                        android:layout_width="0dp">
                
                        <TextView
                            android:id="@+id/viewchainprod"
                            android:textSize="15sp"
                            android:textStyle="bold"
                            android:layout_width="fill_parent"
                            android:layout_height="40dp"
                            android:background="@android:color/white"
                            android:gravity="center"
                            android:textColor="@android:color/black"
                            android:text="@string/pdct" />
                
                    </LinearLayout>
                
                    <LinearLayout
                        android:layout_weight="1"
                        android:layout_height="fill_parent"
                        android:layout_width="0dp"
                        android:layout_marginStart="1dp">
                
                        <TextView
                            android:id="@+id/viewchainthick"
                            android:textSize="15sp"
                            android:textStyle="bold"
                            android:layout_width="fill_parent"
                            android:layout_height="40dp"
                            android:background="@android:color/white"
                            android:gravity="center"
                            android:textColor="@android:color/black"
                            android:text="@string/thcns"/>
                    </LinearLayout>
                
                    <LinearLayout
                        android:layout_weight="1"
                        android:layout_height="fill_parent"
                        android:layout_width="0dp"
                        android:layout_marginStart="1dp">
                
                        <TextView
                            android:id="@+id/viewchainsize"
                            android:textSize="15sp"
                            android:textStyle="bold"
                            android:layout_width="fill_parent"
                            android:layout_height="40dp"
                            android:background="@android:color/white"
                            android:gravity="center"
                            android:textColor="@android:color/black"
                            android:text="@string/size"/>
                    </LinearLayout>
                    <LinearLayout
                        android:layout_weight="1"
                        android:layout_height="fill_parent"
                        android:layout_width="0dp"
                        android:layout_marginStart="1dp">
                
                        <TextView
                            android:id="@+id/viewchainsqft"
                            android:textSize="15sp"
                            android:textStyle="bold"
                            android:layout_width="fill_parent"
                            android:layout_height="40dp"
                            android:background="@android:color/white"
                            android:gravity="center"
                            android:textColor="@android:color/black"
                            android:text="@string/sqft"/>
                
                    </LinearLayout>
                </LinearLayout>
                
                

                【讨论】:

                  【解决方案14】:

                  中间部分的笔划加倍,我使用了这个图层列表drawable:

                  <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
                  
                   <item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
                  
                       <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
                         <solid android:color="@color/grey" />
                      </shape>
                  </item>
                  
                  <item android:top="1dp" android:left="1dp" android:bottom="1dp" android:right="1dp">
                  
                      <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
                        <solid android:color="@color/lightgrey" />
                      </shape>
                   </item>
                  </layer-list>
                  

                  【讨论】:

                    【解决方案15】:

                    我认为最好创建 1px 九个补丁图像,并在 TableRow 和 TableLayout 中使用 showDividers 属性,因为它们都是 LinearLayouts

                    【讨论】:

                    • 你能举个简单的例子吗?
                    【解决方案16】:

                    在上述答案中,单元格之间的边界加倍。 所以,你可以试试这个解决方案:

                    <item
                        android:left="-1dp"
                        android:top="-1dp">
                    
                        <shape xmlns:android="http://schemas.android.com/apk/res/android"
                               android:shape="rectangle">
                            <solid android:color="#fff"/>
                            <stroke
                                android:width="1dp"
                                android:color="#ccc"/>
                        </shape>
                    </item>
                    

                    【讨论】:

                      【解决方案17】:

                      另一种解决方案是使用线性布局并在行和单元格之间设置分隔符,如下所示:

                      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:orientation="vertical" android:layout_width="match_parent"
                      android:layout_height="match_parent">
                      
                      <View
                          android:layout_width="match_parent"
                          android:layout_height="1px"
                          android:background="#8000"/>
                      
                      <LinearLayout
                          android:orientation="horizontal"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:layout_weight="1">
                      
                          <View
                              android:layout_width="@dimen/border"
                              android:layout_height="match_parent"
                              android:background="#8000"
                              android:layout_marginTop="1px"
                              android:layout_marginBottom="1px"/>
                      
                          <LinearLayout
                              android:orientation="horizontal"
                              android:layout_width="match_parent"
                              android:layout_height="match_parent"
                              android:layout_weight="1"
                              ></LinearLayout>
                      
                          <View
                              android:layout_width="@dimen/border"
                              android:layout_height="match_parent"
                              android:background="#8000"
                              android:layout_marginTop="1px"
                              android:layout_marginBottom="1px"/>
                      
                          <LinearLayout
                              android:orientation="horizontal"
                              android:layout_width="match_parent"
                              android:layout_height="match_parent"
                              android:layout_weight="1"></LinearLayout>
                      
                          <View
                              android:layout_width="@dimen/border"
                              android:layout_height="match_parent"
                              android:background="#8000"
                              android:layout_marginTop="1px"
                              android:layout_marginBottom="1px"/>
                      
                      </LinearLayout>
                      
                      <View
                          android:layout_width="match_parent"
                          android:layout_height="1px"
                          android:background="#8000"/>
                      
                      <LinearLayout
                          android:orientation="horizontal"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:layout_weight="1">
                      
                          <View
                              android:layout_width="@dimen/border"
                              android:layout_height="match_parent"
                              android:background="#8000"
                              android:layout_marginTop="1px"
                              android:layout_marginBottom="1px"/>
                      
                          <LinearLayout
                              android:orientation="horizontal"
                              android:layout_width="match_parent"
                              android:layout_height="match_parent"
                              android:layout_weight="1"
                              ></LinearLayout>
                      
                          <View
                              android:layout_width="@dimen/border"
                              android:layout_height="match_parent"
                              android:background="#8000"
                              android:layout_marginTop="1px"
                              android:layout_marginBottom="1px"/>
                      
                          <LinearLayout
                              android:orientation="horizontal"
                              android:layout_width="match_parent"
                              android:layout_height="match_parent"
                              android:layout_weight="1"></LinearLayout>
                          <View
                              android:layout_width="@dimen/border"
                              android:layout_height="match_parent"
                              android:background="#8000"
                              android:layout_marginTop="1px"
                              android:layout_marginBottom="1px"/>
                      </LinearLayout>
                      
                      <View
                          android:layout_width="match_parent"
                          android:layout_height="1px"
                          android:background="#8000"/>
                      </LinearLayout>
                      

                      这是一个肮脏的解决方案,但它很简单,也适用于透明背景和边框。

                      【讨论】:

                        【解决方案18】:

                        我知道这是一个老问题......无论如何......如果你想让你的 xml 保持美观和简单,你可以扩展 TableLayout 并覆盖 dispatchDraw 来做一些自定义绘图。

                        这是一个快速而肮脏的实现,它在表格视图周围绘制一个矩形以及水平和垂直条:

                        public class TableLayoutEx extends TableLayout {
                            private Paint linePaint = null;
                            private Rect tableLayoutRect;
                        
                            public TableLayoutEx(Context context) {
                                super(context);
                            }
                        
                            public TableLayoutEx(Context context, AttributeSet attrs) {
                                super(context, attrs);
                            }
                        
                            @Override
                            protected void onSizeChanged(int w, int h, int oldw, int oldh) {
                                super.onSizeChanged(w, h, oldw, oldh);
                        
                                float strokeWidth = this.getContext().getResources().getDisplayMetrics().scaledDensity * 1;
                                linePaint = new Paint(0);
                                linePaint.setColor(0xff555555);
                                linePaint.setStrokeWidth(strokeWidth);
                                linePaint.setStyle(Paint.Style.STROKE);
                        
                                Rect rect = new Rect();           
                                int paddingTop= getPaddingTop();
                        
                                this.getDrawingRect(rect);
                                tableLayoutRect = new Rect(rect.left, rect.top + paddingTop, rect.right, rect.bottom);
                            }
                        
                            @Override
                            protected void dispatchDraw(Canvas canvas) {
                                super.dispatchDraw(canvas);
                        
                                Rect rect = new Rect();
                        
                                if (linePaint != null) {
                                    canvas.drawRect(tableLayoutRect, linePaint);
                                    float y = tableLayoutRect.top;
                                    for (int i = 0; i < getChildCount() - 1; i++) {
                                        if (getChildAt(i) instanceof TableRow) {
                                            TableRow tableRow = (TableRow) getChildAt(i);
                                            tableRow.getDrawingRect(rect);
                                            y += rect.height();
                                            canvas.drawLine(tableLayoutRect.left, y, tableLayoutRect.right, y, linePaint);
                                            float x = tableLayoutRect.left;
                                            for (int j = 0; j < tableRow.getChildCount() - 1; j++) {
                                                View view = tableRow.getChildAt(j);
                                                if (view != null) {
                                                    view.getDrawingRect(rect);
                                                    x += rect.width();
                                                    canvas.drawLine(x, tableLayoutRect.top, x, tableLayoutRect.bottom, linePaint);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        

                        带有第三列环绕文本的xml示例:

                        <com.YOURPACKAGE.TableLayoutEx
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:shrinkColumns="2"
                            android:paddingTop="6dp">
                        
                            <TableRow>
                                <TextView
                                    android:text="@string/my_text_0_0"
                                    android:padding="@dimen/my_padding"/>
                                <TextView
                                    android:text="@string/my_text_0_1"
                                    android:padding="@dimen/my_padding"/>
                                <TextView                   
                                    android:text="@string/my_text_0_2_to_wrap"
                                    android:padding="@dimen/my_padding"/>
                            </TableRow>
                        
                            <!--more table rows here-->
                        
                        </com.YOURPACKAGE.TableLayoutEx>
                        

                        【讨论】:

                          猜你喜欢
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 2022-08-04
                          • 1970-01-01
                          • 1970-01-01
                          • 2016-02-26
                          • 1970-01-01
                          相关资源
                          最近更新 更多