2015-10-11 40 views
0

我在这里看过类似的问题,但没有一个清楚我的怀疑。如何在Android中设置表格行的边框?

我已阅读有关为每个单元格,表格行等添加边框的信息。但我需要在某些行集合周围有一个边框(类型框)。

让我们假设一个表有5行,但我想将3行放在一个单独的框中,其他2行放在一个单独的框中。

我想从后面的java代码以编程方式做它;可能吗? enter image description here

其中有球体的矩形是tablerow的元素,我为某些表行设置了绿色和红色背景。我希望它能像边框一样显示,就像包含一组行的框,而不是完整的红色和绿色背景。我希望我的问题清楚。需要你的建议..

回答

2

你可以通过设置backgroundpadding为你的rows.of当然我认为还有其他方式存在。

<TableLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent"> 
    <TableRow 
     android:id="@+id/t1" 
     android:background="#00f" 
     android:padding="2dp" 
     android:layout_margin="5dp"> 
     <TextView 
      android:text=" blue border " 
      android:gravity="center" 
      android:textColor="#000" 
      android:background="#fff" 
      android:padding="5dp"/> 
    </TableRow> 
    <TableRow 
     android:id="@+id/t2" 
     android:background="#f00" 
     android:padding="2dp" 
     android:layout_margin="5dp"> 
     <TextView 
      android:text="red border" 
      android:textColor="#000" 
      android:gravity="center" 
      android:background="#fff" 
      android:padding="5dp"/> 
    </TableRow> 
    <TableRow 
     android:id="@+id/t3" 
     android:background="#0f0" 
     android:padding="2dp" 
     android:layout_margin="5dp"> 
     <TextView 
      android:text="green border" 
      android:textColor="#000" 
      android:gravity="center" 
      android:background="#fff" 
      android:padding="5dp"/> 
    </TableRow> 
</TableLayout> 

enter image description here

UPDATE:

在你的活动,你可以通过TabaleRow ID更改边框颜色:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    TableRow row1 = (TableRow) findViewById(R.id.t1); 
    row1.setBackgroundColor(Color.BLUE); 

    TableRow row2 = (TableRow) findViewById(R.id.t2); 
    row2.setBackgroundColor(Color.BLUE); 

    TableRow row3 = (TableRow) findViewById(R.id.t3); 
    row3.setBackgroundColor(Color.RED); 

} 
+0

这又是围绕每个表行的边框。我想要它围绕一组行。另外,你可以提供一些方法来从java编程的方式吗? –

+1

您可以为每个“TableRow”和您的活动setBackgrounColor中的每一个设置“id”。像这样:tableRow1.setBackgroundColor(Color.BLUE); – MHP

+0

你能解释一下吗?此外,我的行中包含带drawableTop图像的按钮,因此应用颜色会很小。你能提供一个更具体的建议吗? –

相关问题