Dav*_*sse 192
我对这个问题的解决方案是在每个单元格的背景字段上放置一个xml可绘制资源.通过这种方式,您可以为所有单元格定义具有所需边框的形状.唯一的不便是极端单元格的边界宽度是其他单元格的一半,但如果您的桌面填满整个屏幕,则没有问题.
一个例子:
绘制/ 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>
Run Code Online (Sandbox Code Playgroud)
布局/ 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>
Run Code Online (Sandbox Code Playgroud)
编辑:一个例子

Edit2:另一个例子(包含更多元素:圆角,渐变......)

我在http://blog.intelligenia.com/2012/02/programacion-movil-en-android.html#more中详细解释了这个问题.它是西班牙语,但有一些更复杂的表格的代码和图像.
Sha*_*awn 53
我不得不同意布拉德.那是一个糟糕的答案.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>
Run Code Online (Sandbox Code Playgroud)
Mic*_*ray 39
如果你只想在行之间插入一行(例如,在"Total"行之上),那么有一个简单的解决方案 - 只需添加一个具有背景颜色和特定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>
Run Code Online (Sandbox Code Playgroud)
设置android:layout_height="1px"或厚度你想要的边框.根据需要填写尽可能多的空TextView列以匹配表的其余部分,或者只使用一个,android:layout_span如我所示.
输出看起来像这样:

如果您尝试添加更复杂的边框,则已发布的其他答案更合适.
Yas*_*gar 25
我想要的是这样一张桌子

我在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>
Run Code Online (Sandbox Code Playgroud)
然后在我的表布局中:
<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>
..........
.......
......
Run Code Online (Sandbox Code Playgroud)
小智 21
你也可以通过xml来做这个,而不是通过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);
Run Code Online (Sandbox Code Playgroud)
小智 14
要在不编写Java代码的情况下围绕每个单元格进行1dp折叠边框,而不创建带<shape...>标记的其他xml布局,可以尝试以下解决方案:
在<TableLayout...>添加
android:background="#CCC"和android:paddingTop="1dp"和android:stretchColumns="0"
在<TableRow...>添加
android:background="#CCC"和android:paddingBottom="1dp"和android:paddingRight="1dp"
在TableRow中的每个单元格/子项中,即<TextView...>添加
android:background="#FFF"和android:layout_marginLeft="1dp"
如上所述,遵循填充和边距非常重要.此解决方案将在(X)HTML/CSS中绘制1dp边框aka border-collapse属性.
填充表格单元格中的背景颜色<TableLayout...>并<TableRow...>表示边框线颜色和背景<TextView...>.如有必要,您可以在单元格中添加一些填充.
这里有一个例子:
<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>
Run Code Online (Sandbox Code Playgroud)
Moh*_*j S 13

在这里,我通过以下设计图像设计了列表.我的listitem文件名是Propertylistitem.xml,cellborder.xml用于cellborder输出的drawable形状,显示在此图像中.我在这里添加了必要的代码
文件名:propertylistitem.xml
<TableLayout... >
<TableRow... >
<TextView ...
android:background="@drawable/cellborder"
android:text="Amount"/>
</TableRow>
<TableRow... >
<TextView...
android:background="@drawable/cellborder"
android:text="5000"/>
</TableRow>
</TableLayout>
Run Code Online (Sandbox Code Playgroud)
filename: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>
Run Code Online (Sandbox Code Playgroud)
小智 8
经过长时间的搜索和数小时的尝试,这是我可以做的最简单的代码:
ShapeDrawable border = new ShapeDrawable(new RectShape());
border.getPaint().setStyle(Style.STROKE);
border.getPaint().setColor(Color.BLACK);
tv.setBackground(border);
content.addView(tv);
Run Code Online (Sandbox Code Playgroud)
tv是一个带有简单文本的TextView,内容是我的容器(在本例中为LinearLayout).这有点容易.
那可能会激发你的灵感.这些步骤展示了如何动态创建边界表
这是表格视图
<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>
Run Code Online (Sandbox Code Playgroud)
这里使用"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>
Run Code Online (Sandbox Code Playgroud)
我们可以将这个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>
Run Code Online (Sandbox Code Playgroud)
最后这里是用Kotlin编写的紧凑代码,但如果需要的话很容易将其转换为java
well temps是一个包含数据的数组列表: ArrayList<Double>()
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()
}
Run Code Online (Sandbox Code Playgroud)
你可以在你的片段中使用它,例如像这样
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
table = view?.findViewById<View>(R.id.simpleTableLayout) as TableLayout
CreateTable()
}
Run Code Online (Sandbox Code Playgroud)
如何重写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);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您需要带边框的表格,我建议使用带权重的线性布局,而不是 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
340770 次 |
| 最近记录: |