【问题标题】:android table layout - simple exmpleandroid 表格布局 - 简单示例
【发布时间】:2017-02-23 10:24:06
【问题描述】:

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

simple grid I am looking for

如果我可以制作仅显示 3 行高的矩阵并允许用户滚动/翻阅它的“窗口”,那就太酷了。这样,当屏幕旋转时,列表/矩阵在水平模式下不会占用整个屏幕。

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

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

<?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>

【问题讨论】:

    标签: android tablelayout


    【解决方案1】:

    你可以看看this。因此,在您的情况下,您可以在末尾添加以下内容:

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

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

    TableLayout tableLayout = (TableLayout) findViewById(R.id.table);
    
    //TODO: inflate some rows
    
    tableLayout.addView(row1);
    tableLayout.addView(row2);
    

    等等……

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

    <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>
    

    你可以给每个textview一个id,这样当你对每一行进行inflate时,你就可以引用textview来设置你的数据。

    我认为另一个需要注意的重要事项是,根据您的数据或数据大小,您可以考虑使用带有 GridLayoutManager 的 RecyclerView 来更有效地实现您想要做的事情。

    【讨论】:

    • 非常感谢。我的数据将不超过 20 行深和 4 列宽。它不是很大,但带有线条的网格会更容易阅读。我希望它适合我的屏幕底部,同时我将文本视图保留在我的应用程序中......在顶部。我应该使用 tableLayout 还是 recyclerView?由于我没有使用过它们,所以它们对我来说都是新的。
    • 为了不写重复的代码,我认为RecyclerView可能是更好的选择。网上有很多关于如何开始使用它的教程。但简而言之,您将数据输入到适配器中,该适配器会将数据与它应该显示的视图进行映射。如果你觉得 RecyclerView 太复杂,你也可以看看 GridView。但是,最好尝试使用 RecyclerView,因为它更有效(ViewHolder 模式)。
    • 你可以看看这个答案:stackoverflow.com/questions/40587168/…
    • 再次感谢您的帮助。我用红色块实现了简单的 RecyclerView 教程。有效!我将列数减少到 3 并调整了背景颜色。我想修改它,以便根据我加载的文本自动设置列宽。在上面的原始帖子中,我添加了我正在寻找的简单格式的图像。我很感激任何帮助。今天的文本是一组对象(列表)。我只想将每个对象行中的测试分配给recyclerview。谢谢
    • 因此,我认为与其使用我之前建议的 GridLayoutManager,不如将 LinearLayoutManager 与 RecyclerView 一起使用会更好。您可以定义具有三个水平放置的文本视图的布局。假设您的模型是时间/指令(您可以使用位置来定义您的“项目”列),对于每一行,只需根据您传递给适配器的模型列表相应地设置文本视图。如果您需要进一步说明,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 2011-06-12
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    相关资源
    最近更新 更多