如何在TableLayout周围添加边框?

问题描述 投票:0回答:2

下面是我的表代码。我的屏幕看起来像这样 https://i.stack.imgur.com/U5ppn.jpg 但我想让它看起来像这样 https://i.stack.imgur.com/gFQZw.jpg。如何在每行和表格布局周围添加边框?

<TableLayout
    android:id="@+id/table2"
    android:layout_width="fill_parent"
    android:layout_below="@+id/test_button_text23"
    android:layout_marginLeft="45dp"
    android:layout_marginBottom="25dp"
    android:layout_marginRight="45dp"    
    android:layout_height="fill_parent"
    android:stretchColumns="*" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:gravity="left"
            android:text="Quantity"
            android:textStyle="bold" />

        <TextView
            android:gravity="center"
            android:textStyle="bold"
            android:text="Item" />

    </TableRow>

</TableLayout>     

 

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/localTime"
        android:textColor="#000000"
        android:gravity="left" />

    <TextView
        android:id="@+id/apprentTemp"
        android:textColor="#000000"
        android:gravity="center" />

</TableRow>

 

View row = getLayoutInflater().inflate(R.layout.rows, null);
((TextView) row.findViewById(R.id.localTime)).setText(item.getString("Item"));
((TextView) row.findViewById(R.id.apprentTemp)).setText(item.getString("Quantity"));
android android-tablelayout
2个回答
48
投票

为了在表格行和表格布局周围创建边框,您需要创建一个可绘制对象作为边框,然后将其设置为行的背景。

例如:

res/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="#ffffff"/>
   <stroke android:width="1dp" android:color="#000000"/>
</shape>

res/layout/your_layout.xml

<TableLayout
     android:id="@+id/table2"
     android:layout_width="fill_parent"
     android:layout_below="@+id/test_button_text23"
     android:layout_marginLeft="45dp"
     android:layout_marginBottom="25dp"
     android:layout_marginRight="45dp"
     android:layout_height="fill_parent"
     android:stretchColumns="*">

     <TableRow
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@drawable/border">

           <TextView
              android:gravity="left"
              android:text="Quantity"
              android:background="@drawable/border"
              android:textStyle="bold"/>

           <TextView
              android:gravity="center"
              android:textStyle="bold"
              android:background="@drawable/border"
              android:text="Item" />

     </TableRow>

</TableLayout>  

这看起来与您发布的图片不完全一样,但请尝试一下以获得您想要的东西。


2
投票

你可以试试这个代码。这是一个有效的代码。

TableLayout.java

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".TableViewActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center"
        android:padding="10dp"
        android:text="@string/table_layout_title"
        android:textSize="23sp"
        android:textStyle="bold" />

    <TableLayout
        android:id="@+id/tableLayoutId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:stretchColumns="1">

        <TableRow

            android:id="@+id/firstRow"
            android:background="@drawable/border"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="37dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:layout_marginEnd="1dp"
                android:layout_marginBottom="2dp"
                android:layout_weight="1"
                android:background="#b0b0b0"
                android:gravity="center"
                android:paddingStart="3dp"
                android:paddingTop="10dp"
                android:paddingEnd="3dp"
                android:paddingBottom="10dp"
                android:text="Name"
                android:textColor="@android:color/white"
                android:textSize="12sp"
                android:textStyle="bold" />

            <TextView
                android:layout_marginTop="2dp"
                android:layout_marginBottom="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginEnd="1dp"
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:background="#b0b0b0"
                android:gravity="center"
                android:paddingStart="3dp"
                android:paddingTop="10dp"
                android:paddingEnd="3dp"
                android:paddingBottom="10dp"
                android:text="Father Name"
                android:textColor="@android:color/white"
                android:textSize="12sp"
                android:textStyle="bold" />

            <TextView
                android:layout_marginTop="2dp"
                android:layout_marginBottom="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginEnd="1dp"
                android:id="@+id/textView3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:background="#b0b0b0"
                android:gravity="center"
                android:paddingStart="3dp"
                android:paddingTop="10dp"
                android:paddingEnd="3dp"
                android:paddingBottom="10dp"
                android:text="Mother Name"
                android:textColor="@android:color/white"
                android:textSize="12sp"
                android:textStyle="bold" />

            <TextView
                android:layout_marginTop="2dp"
                android:layout_marginBottom="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginEnd="1dp"
                android:id="@+id/textView4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:background="#b0b0b0"
                android:gravity="center"
                android:paddingStart="3dp"
                android:paddingTop="10dp"
                android:paddingEnd="3dp"
                android:paddingBottom="10dp"
                android:text="School Name"
                android:textColor="@android:color/white"
                android:textSize="12sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:layout_marginEnd="1dp"
                android:layout_marginBottom="2dp"
                android:layout_weight="1.1"
                android:background="#b0b0b0"
                android:gravity="center"
                android:paddingStart="3dp"
                android:paddingTop="10dp"
                android:paddingEnd="3dp"
                android:paddingBottom="10dp"
                android:text="Phone"
                android:textColor="@android:color/white"
                android:textSize="12sp"
                android:textStyle="bold" />

        </TableRow>

    </TableLayout>

</LinearLayout>

@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="#FF0303"/>
    <stroke android:width="1dp" android:color="#000000"/>
</shape>
© www.soinside.com 2019 - 2024. All rights reserved.