android表格布局 - 简单的例子 [英] android table layout - simple exmple

查看:36
本文介绍了android表格布局 - 简单的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在屏幕底部放置一个简单的单一网格.这是所需结果的屏幕截图

如果我可以制作显示矩阵只有 3 行高的窗口"并允许用户滚动/浏览它,那将会很酷.这样,当屏幕旋转时,列表/矩阵不会在水平模式下占用整个屏幕.

我怀疑我应该使用表格布局吗?下面是我的activity_main.xml.我想用包含多行 3 列文本的表格布局替换最底部/第三个文本视图.

显然我可以拖放小部件,但如何填充行和列?

<文本视图机器人:文本=ABC"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/textView2"android:layout_alignParentTop="true"机器人:layout_centerHorizo​​ntal="true"android:textAlignment="center"/><天文台android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/def"机器人:layout_alignParentLeft =真"android:layout_alignParentStart="true"/><文本视图android:id="@+id/textView"android:layout_width="match_parent"android:layout_height="wrap_content"机器人:滚动条=垂直"机器人:重力=底部"机器人:文本=日志:"机器人:maxLines =8"android:layout_marginTop="31dp"android:layout_below="@+id/textView2"机器人:layout_alignParentLeft =真"android:layout_alignParentStart="true"/><文本视图机器人:文本=吉"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="19dp"android:id="@+id/textView3"机器人:layout_alignParentBottom =真"android:layout_centerHorizo​​ntal="true"/></RelativeLayout>

解决方案

可以看一下 这个.因此,在您的情况下,您可以在末尾添加以下内容:

在您的代码中,您可以获取 TableLayout 的引用,然后将行膨胀为三列,然后将行添加到您的 TableLayout:

TableLayout tableLayout = (TableLayout) findViewById(R.id.table);//TODO: 增加一些行tableLayout.addView(row1);tableLayout.addView(row2);

等等...

膨胀后的行布局如下所示:

<文本视图android:id="@+id/第一个"机器人:文本=第一"android:padding="3dip"/><文本视图android:id="@+id/秒"机器人:文本=第二"机器人:重力=中心"android:padding="3dip"/><文本视图android:id="@+id/第三个"机器人:文本=第三"机器人:重力=正确"android:padding="3dip"/></TableRow>

你可以给每个 textview 一个 id,这样当你膨胀每一行时,你可以引用 textview 来设置你的数据.

我认为另一件需要注意的重要事情是,根据您的数据或数据大小,您可以考虑将 RecyclerView 与 GridLayoutManager 结合使用,以更有效地实现您的目标.

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>

解决方案

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.

这篇关于android表格布局 - 简单的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆