简体   繁体   中英

How can make table for my android app

在此处输入图片说明

I want to make such kind of table(as shown in the picture) for my android application. But i can't figure it out. How can i do it? Please give me some sample example or tricks that will help me to do this table.

Layout which you are expected can be obtained with LinearLayout with attributes layout_weight also. But as you are more familiar with TableLayout .

TableLayout:

 <?xml version="1.0" encoding="utf-8"?>

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="2"
    android:stretchColumns="2" >

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Text1" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Text2" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Text3" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Text4" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="@android:color/black"
                  />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Text5" />
        </LinearLayout>
    </TableRow>
</TableLayout>

One two Tablerows are displayed as per SO Layout, you can take further long from here.

You can use tablelayout. See this example http://www.mkyong.com/android/android-tablelayout-example/

its quite simple. the android:layout-weight property should do the job. but you also need the borders so we have to create a drawable to set the background for TextView s inside /res/drawable folder :

bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <stroke
        android:width="2dp"
        android:color="#000" />

</shape>

and here is the activity's xml

layout_main.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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bg"
            android:gravity="center"
            android:text="@string/hello_world" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bg"
            android:gravity="center"
            android:text="@string/hello_world" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bg"
            android:gravity="center"
            android:text="@string/hello_world" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/bg"
                android:gravity="center"
                android:text="@string/hello_world" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/bg"
                android:gravity="center"
                android:text="@string/hello_world" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bg"
            android:gravity="center"
            android:text="@string/hello_world" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/bg"
                android:gravity="center"
                android:text="@string/hello_world" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/bg"
                android:gravity="center"
                android:text="@string/hello_world" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bg"
            android:gravity="center"
            android:text="@string/hello_world" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/bg"
                android:gravity="center"
                android:text="@string/hello_world" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/bg"
                android:gravity="center"
                android:text="@string/hello_world" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

and this is how it looks: 在此处输入图片说明

Hope its what you wanted ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM