web-dev-qa-db-fra.com

Comment créer une table en utilisant TableLayout dans Android Studio?

Quelqu'un peut-il me fournir un exemple de code pour référence? Je veux créer le tableau qui aime la photo ci-dessous.

TABLE:

enter image description here

11
Ka Chun Yim

enter image description here

mettre en forme le fichier xml pour vous, copiez-le et modifiez-le selon votre choix:

<TableLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"

Android:layout_width="fill_parent"
Android:layout_height="match_parent"
    Android:stretchColumns="0,1,2"
    Android:gravity="center">

    <TableRow
        Android:background="#FFFFFF"
        Android:layout_width="fill_parent"
        Android:layout_height="0dp"
        Android:layout_margin="1dp"
        Android:layout_weight="1"
        >
        <TableRow
            Android:background="#000000"
            Android:layout_width="fill_parent"
            Android:layout_height="0dp"
            Android:layout_margin="1dp"
            Android:layout_weight="1"
            >



        </TableRow>
    </TableRow>
<TableRow
    Android:background="#000000"
    Android:layout_width="fill_parent"
    Android:layout_height="0dp"
    Android:layout_margin="1dp"
    Android:layout_weight="1"

    >

    <TextView
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:textAppearance="?android:attr/textAppearanceLarge"
        Android:text=" Date "
        Android:layout_margin="1dp"
        Android:layout_column="0"
        Android:background="#FFFFFF"
        Android:textStyle="bold"
        Android:gravity="center"
        />

    <TextView
        Android:layout_width="wrap_content"
        Android:layout_height="match_parent"
        Android:textAppearance="?android:attr/textAppearanceLarge"
        Android:text="Miles "
        Android:layout_margin="1dp"
        Android:layout_column="1"
        Android:background="#FFFFFF"
        Android:gravity="center"
        Android:textStyle="bold"
      />

    <TextView
        Android:layout_width="wrap_content"
        Android:layout_height="match_parent"
        Android:textAppearance="?android:attr/textAppearanceLarge"
        Android:text="Calories"
        Android:layout_margin="1dp"
        Android:background="#FFFFFF"
        Android:gravity="center"
        Android:textStyle="bold"
        Android:layout_column="2"
       />
</TableRow>

    <TableRow
        Android:background="#000000"
        Android:layout_width="fill_parent"
        Android:layout_height="0dp"
        Android:layout_margin="1dp"
        Android:layout_weight="1"
        >

        <TextView
            Android:layout_width="match_parent"
            Android:layout_height="match_parent"
            Android:textAppearance="?android:attr/textAppearanceLarge"
            Android:text=" Text"
            Android:layout_margin="1dp"
            Android:layout_column="0"
            Android:background="#FFFFFF"
            Android:gravity="center"
            />

        <TextView
            Android:layout_width="wrap_content"
            Android:layout_height="match_parent"
            Android:textAppearance="?android:attr/textAppearanceLarge"
            Android:text=" Text"
            Android:layout_margin="1dp"
            Android:layout_column="1"
            Android:background="#FFFFFF"
            Android:gravity="center"
            />

        <TextView
            Android:layout_width="wrap_content"
            Android:layout_height="match_parent"
            Android:textAppearance="?android:attr/textAppearanceLarge"
            Android:text=" Text"
            Android:layout_margin="1dp"
            Android:background="#FFFFFF"
            Android:gravity="center"
            Android:layout_column="2" />
    </TableRow>



    <TableRow
        Android:background="#000000"
        Android:layout_width="fill_parent"
        Android:layout_height="0dp"
        Android:layout_margin="1dp"
        Android:layout_weight="1"
        >

        <TextView
            Android:layout_width="match_parent"
            Android:layout_height="match_parent"
            Android:textAppearance="?android:attr/textAppearanceLarge"
            Android:text=" Text"
            Android:layout_margin="1dp"
            Android:layout_column="0"
            Android:background="#FFFFFF"
            Android:gravity="center"
            />

        <TextView
            Android:layout_width="wrap_content"
            Android:layout_height="match_parent"
            Android:textAppearance="?android:attr/textAppearanceLarge"
            Android:text=" Text"
            Android:layout_margin="1dp"
            Android:layout_column="1"
            Android:background="#FFFFFF"
            Android:gravity="center"
            />

        <TextView
            Android:layout_width="wrap_content"
            Android:layout_height="match_parent"
            Android:textAppearance="?android:attr/textAppearanceLarge"
            Android:text=" Text"
            Android:layout_margin="1dp"
            Android:background="#FFFFFF"
            Android:gravity="center"
            Android:layout_column="2" />
    </TableRow>

    <TableRow
        Android:background="#000000"
        Android:layout_width="fill_parent"
        Android:layout_height="0dp"
        Android:layout_margin="1dp"
        Android:layout_weight="1"
        >

        <TextView
            Android:layout_width="match_parent"
            Android:layout_height="match_parent"
            Android:textAppearance="?android:attr/textAppearanceLarge"
            Android:text=" Text"
            Android:layout_margin="1dp"
            Android:layout_column="0"
            Android:background="#FFFFFF"
            Android:gravity="center"
            />

        <TextView
            Android:layout_width="wrap_content"
            Android:layout_height="match_parent"
            Android:textAppearance="?android:attr/textAppearanceLarge"
            Android:text=" Text"
            Android:layout_margin="1dp"
            Android:layout_column="1"
            Android:background="#FFFFFF"
            Android:gravity="center"
            />

        <TextView
            Android:layout_width="wrap_content"
            Android:layout_height="match_parent"
            Android:textAppearance="?android:attr/textAppearanceLarge"
            Android:text=" Text"
            Android:layout_margin="1dp"
            Android:background="#FFFFFF"
            Android:gravity="center"
            Android:layout_column="2" />
    </TableRow>


</TableLayout>
19
Androider

essayez ce code il vous aidera

simple_tablelayout.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">
<TableLayout
    Android:id="@+id/tabla_cuerpo"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content">
    <TableRow
        Android:id="@+id/tableRow1"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content">
        <TextView
            Android:id="@+id/id_cad_details_dialog_key"
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:background="@drawable/cell_shape"
            Android:padding="5dp"
            Android:textColor="#000"
            Android:text="TextView">
        </TextView>
        <TextView
            Android:id="@+id/id_cad_details_dialog_value"
            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>

cell_shape.xml

<shape
    xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:shape= "rectangle"  >
    <solid Android:color="#fff"/>
    <stroke Android:width="1dp"
        Android:color="#000"/>
</shape>
1
balu

Une disposition qui organise ses enfants en lignes et colonnes. Un TableLayout se compose d'un certain nombre d'objets TableRow, chacun définissant une ligne (en fait, vous pouvez avoir d'autres enfants, qui seront expliqués ci-dessous). Les conteneurs TableLayout n'affichent pas de lignes de bordure pour leurs lignes, colonnes ou cellules.

<TableLayout
            xmlns:Android="http://schemas.Android.com/apk/res/Android"
            Android:layout_width="match_parent"
            Android:layout_height="match_parent"
            Android:shrinkColumns="*"  Android:stretchColumns="*" Android:background="#ffffff">


                        <!-- Row 1 with single column -->
                        <TableRow
                            Android:layout_height="wrap_content"
                            Android:layout_width="fill_parent"
                            Android:gravity="center_horizontal">





                       </TableRow>

                        <!-- Row 2 with 3 columns -->


                        <TableRow
                            Android:id="@+id/tableRow1"
                            Android:layout_height="wrap_content"
                            Android:layout_width="match_parent">

                            <TextView
                                Android:id="@+id/TextView04" Android:text="DATE"
                                Android:layout_weight="1" Android:background="#dcdcdc"
                                Android:textColor="#000000"
                                Android:padding="20dip" Android:gravity="center"/>

                            <TextView
                                Android:id="@+id/TextView04" Android:text="Miles"
                                Android:layout_weight="1" Android:background="#d3d3d3"
                                Android:textColor="#000000"
                                Android:padding="20dip" Android:gravity="center"/>

                            <TextView
                                Android:id="@+id/TextView04" Android:text="Calories"
                                Android:layout_weight="1" Android:background="#cac9c9"
                                Android:textColor="#000000"
                                Android:padding="20dip" Android:gravity="center"/>

                        </TableRow>

<!-- Row 3 with 3 columns -->


                        <TableRow
                            Android:id="@+id/tableRow2"
                            Android:layout_height="wrap_content"
                            Android:layout_width="match_parent">

                            <TextView
                                Android:text=""
                                Android:layout_weight="1" Android:background="#dcdcdc"
                                Android:textColor="#000000"
                                Android:padding="20dip" Android:gravity="center"/>

                            <TextView
                                Android:text=""
                                Android:layout_weight="1" Android:background="#d3d3d3"
                                Android:textColor="#000000"
                                Android:padding="20dip" Android:gravity="center"/>

                            <TextView

                                Android:layout_weight="1" Android:background="#cac9c9"
                                Android:textColor="#000000"
                                Android:padding="20dip" Android:gravity="center"/>

                        </TableRow>



      </TableLayout>
1
IntelliJ Amiya