android table 布局 - 简单示例
android table layout - simple exmple
我想在屏幕底部放置一个简单的单一网格。这是所需结果的屏幕截图
如果我可以使 "Window" 显示仅 3 行高的矩阵并允许用户 scroll/fling 通过它,那就太好了。这样,当屏幕旋转时,list/matrix 不会在水平模式下占用整个屏幕。
我怀疑我应该使用 table 布局?下面是我的activity_main.xml。我想用 table 布局替换最底部/第三个文本视图,该布局包含 3 列多行文本。
显然我可以 drag/drop 小部件,但如何填充行和列?
<?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>
你可以看看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来设置你的数据。
我认为另一件需要注意的重要事情是,根据您的数据或数据大小,您可以考虑将 RecyclerView 与 GridLayoutManager 结合使用,以更有效地实现您想要做的事情。
我想在屏幕底部放置一个简单的单一网格。这是所需结果的屏幕截图
如果我可以使 "Window" 显示仅 3 行高的矩阵并允许用户 scroll/fling 通过它,那就太好了。这样,当屏幕旋转时,list/matrix 不会在水平模式下占用整个屏幕。
我怀疑我应该使用 table 布局?下面是我的activity_main.xml。我想用 table 布局替换最底部/第三个文本视图,该布局包含 3 列多行文本。
显然我可以 drag/drop 小部件,但如何填充行和列?
<?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>
你可以看看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来设置你的数据。
我认为另一件需要注意的重要事情是,根据您的数据或数据大小,您可以考虑将 RecyclerView 与 GridLayoutManager 结合使用,以更有效地实现您想要做的事情。