android table layout - simple exmple

人走茶凉 提交于 2020-01-17 06:01:22

问题


I want to place a simple, single, grid at the bottom of my screen. Here is a screen shot of the desired result

simple grid I am looking for

It would be cool if I could make the "Window" that shows the matrix only 3 rows high and allow the user to scroll/fling through it. With this, when the screen is rotated, the list/matrix would not consume the whole screen in horizontal mode.

I suspect I should use table layout? Below is my activity_main.xml. I want to replace the bottom-most / 3rd textview with a table layout that contains text in 3 columns with multiple rows.

Obviously I can drag/drop the widget but how do I populate the rows and columns?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    tools:context="com.example.ftonyan.gps7.MainActivity">

    <TextView
        android:text="abc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textAlignment="center" />

    <Chronometer
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/def"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:gravity="bottom"
        android:text="Log:"
        android:maxLines="8"
        android:layout_marginTop="31dp"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:text="ghi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="19dp"
        android:id="@+id/textView3"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

回答1:


You can take a look at this. So in your case, you can add the following at the end:

<TableLayout
    android:id="table"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"/>

In your code, you can then get the reference of the TableLayout, then inflate the row with three columns then add the rows to your TableLayout:

TableLayout tableLayout = (TableLayout) findViewById(R.id.table);

//TODO: inflate some rows

tableLayout.addView(row1);
tableLayout.addView(row2);

etc...

Your inflated row layout would look something like the following:

<TableRow>
        <TextView
            android:id="@+id/first"
            android:text="first"
            android:padding="3dip" />
        <TextView
            android:id="@+id/second"
            android:text="second"
            android:gravity="center"
            android:padding="3dip" />
        <TextView
            android:id="@+id/third"
            android:text="third"
            android:gravity="right"
            android:padding="3dip" />
</TableRow>

You can give each textview an id, so that when you inflate each row, you can reference the textview to set your data.

I think another important thing to note is that, depending on your data or data size, you can consider using a RecyclerView with a GridLayoutManager to more efficiently achieve what you're trying to do.



来源:https://stackoverflow.com/questions/42354710/android-table-layout-simple-exmple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!