Border Lines for cells in GridLayout, TableLayout, or GridView?

邮差的信 提交于 2021-02-06 11:03:53

问题


I am trying to create a table/grid for some items within my app and I would like to have a border around each cell to divide the items up and have a coherent association of the setting with the item. This app will be used in an industrial setting where there may be people unfamiliar with Android that need to use this, thus trying to make it as easy as possible.

The table/grid will contain TextView, EditText, Spinner, and Button, and will also be scrollable (via ScrollView parent).

I read about the GridView and found that it (seems) to only be able to get items programmatically, please correct me if I am wrong. I felt that this was unnecessary since I know what items I want and where. Also, I have not tried adding items to a layout programmatically yet so I figured I would try the other options first. Also, the GridView documentation does not say one way or the other if border lines are automatically shown, or if you can have them shown at all.

I started with a TableLayout and was able to get everything except the border lines to work. I tried android:divider to get the lines but that didn't work. One thought I had was to create a bunch of TextViews with black backgrounds and ~2dp widths/heights to make my own border lines. This feels like a huge waste though. Then I also read the TableLayout documentation and found this: "TableLayout containers do not display border lines for their rows, columns, or cells."

I then tried the GridLayout and had the same results as the TableLayout. I tried padding and margins, neither worked. Also, the GridLayout documentation states: "The grid is composed of a set of infinitely thin lines that separate the viewing area into cells."

My questions are:

  1. Is there an attirbute that I missed in TableLayout or GridLayout that will give me border lines via the xml?

  2. If no, then will the GridView give me the lines I want?

  3. Will I be able to add all the perviously mentioned items I want to the GridView?


回答1:


Is there an attirbute that I missed in TableLayout or GridLayout that will give me border lines via the xml?

No.

If no, then will the GridView give me the lines I want?

No.

Will I be able to add all the perviously mentioned items I want to the GridView?

Yes, though how well something like a Spinner will work, I can't say.

The simplest way, off the top of my head, to give you the lines you seek is to have each cell of the TableLayout or GridLayout be some container containing the widget(s) for that cell, where you give the container a background that is your line. A ShapeDrawable could be defined in XML for that background, which will be nicely resizeable based upon the actual requirements of the cell.




回答2:


I was actually able to achieve the desired look by setting the android:background="#000000" within the GridLayout view and then in the child items I set the android:background="#8CDD81" (just some green color) and combined with android:layout_margin="2dp" I was able to get the "grid" lines that I wanted. Thanks to CommonsWare though for getting me thinking in a new direction that turned into a solution.

EDIT: This does not work quite as anticipated. You need the android:layout_alignLeft/Right which are only available via RelativeLayout in order to get just the right width on the child items. Haven't tested this yet using this idea, child items within RelativeLayout within GridLayout.




回答3:


For future visiters this is how I did it with 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>


来源:https://stackoverflow.com/questions/16199625/border-lines-for-cells-in-gridlayout-tablelayout-or-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!