【问题标题】:Border Lines for cells in GridLayout, TableLayout, or GridView?GridLayout、TableLayout 或 GridView 中单元格的边框线?
【发布时间】:2013-04-18 10:54:55
【问题描述】:

我正在尝试为我的应用程序中的某些项目创建一个表格/网格,我希望每个单元格周围都有一个边框来划分项目,并使设置与项目保持一致的关联。这个应用程序将用于工业环境中,可能会有不熟悉 Android 的人需要使用它,因此尽量让它变得简单。

表格/网格将包含 TextViewEditTextSpinnerButton,并且还可以滚动(通过 ScrollView 父级)。

我阅读了GridView 并发现它(似乎)只能以编程方式获取项目,如果我错了,请纠正我。我觉得这是不必要的,因为我知道我想要什么以及在哪里。另外,我还没有尝试以编程方式将项目添加到布局中,所以我想我会先尝试其他选项。此外,GridView documentation 并没有说明是否自动显示边框线,或者您是否可以显示它们。

我从TableLayout 开始,并且能够使除边界线之外的所有内容都正常工作。我尝试android:divider 获取线路,但没有奏效。我的一个想法是创建一堆TextViews,黑色背景和~2dp 宽度/高度来制作我自己的边框线。不过,这感觉像是一种巨大的浪费。然后我还阅读了TableLayout documentation,发现:“TableLayout 容器不会为其行、列或单元格显示边框线。”

然后我尝试了GridLayout,结果与TableLayout 相同。我试过paddingmargins,都没有成功。此外,GridLayout documentation 声明:“网格由一组无限细的线组成,将查看区域划分为多个单元格。”

我的问题是:

  1. TableLayoutGridLayout 中是否有我遗漏的属性会通过xml 给我边界线?

  2. 如果没有,那么GridView 会给我我想要的台词吗?

  3. 我能否将之前提到的所有我想要的项目添加到GridView

【问题讨论】:

    标签: android android-gridview android-tablelayout android-gridlayout


    【解决方案1】:

    我实际上能够通过在GridLayout 视图中设置android:background="#000000" 来实现所需的外观,然后在子项目中我设置android:background="#8CDD81"(只是一些绿色)并结合android:layout_margin="2dp" 我是能够得到我想要的“网格”线。不过,感谢 CommonsWare 让我想到了一个新的方向,最终变成了一个解决方案。

    编辑: 这并不像预期的那样工作。您需要android:layout_alignLeft/Right,它只能通过RelativeLayout 获得,以便在子项上获得正确的宽度。尚未使用此想法进行测试,child itemsRelativeLayout GridLayout 内。

    【讨论】:

    • 感谢您让我朝着正确的方向思考。我能够通过使用(在我的情况下)LinearLayouts 以不同的背景颜色和边距相互嵌套来实现“网格线”效果。
    【解决方案2】:

    是否有我在 TableLayout 或 GridLayout 中遗漏的属性会通过 xml 给我边框线?

    没有。

    如果没有,那么 GridView 会给我想要的线条吗?

    没有。

    我能否将我想要的所有之前提到的项目添加到 GridView 中?

    是的,虽然像 Spinner 这样的东西会有多好,我不能说。

    最简单的方法,在我的脑海中,给你你想要的线条是让TableLayoutGridLayout 的每个单元格都是包含该单元格的小部件的容器,你可以在其中给容器一个背景,即你的线条。 A ShapeDrawable 可以在 XML 中为该背景定义,可以根据单元格的实际要求很好地调整大小。

    【讨论】:

    • 我将研究GridLayout 结合你提到的背景/ShapeDrawable 作为解决方案。谢谢你的信息!
    【解决方案3】:

    对于未来的访问者,这就是我使用 TableLayout 的方式:

    table.xml

    <TableLayout android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:background="#969696">
            <!-- table heading -->
            <TableRow>
                <Button android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Name"
                    android:background="#d2d2d2"
                    android:layout_margin="1dp"
                    />
                <Button android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Address"
                    android:background="#d2d2d2"
                    android:layout_margin="1dp"
                    />
            </TableRow>
            <!-- table data -->
            <TableRow>
                <TextView android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Ahtisham"
                    android:layout_margin="1dp"
                    android:background="#f1f1f1"
                    />
                <TextView android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Kashmir"
                    android:layout_margin="1dp"
                    android:background="#f1f1f1"
                    />
            </TableRow>            
     </TableLayout>
    

    【讨论】:

      猜你喜欢
      • 2015-07-24
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 2014-02-22
      • 2012-06-15
      • 2012-04-04
      • 2013-03-21
      • 2018-10-25
      相关资源
      最近更新 更多