zoukankan      html  css  js  c++  java
  • Android学习之七:使用Container

    3.TableLayout:Android 的TableLayout的布局就像Html的表格一样,可以根据我们的说明来安排widgets的位置。我们可以自己控制屏幕的行数和列数,而每列可以根据包含的内容进行伸缩。

    通常情况下,TableLayout有多个TableRow组成,每个TableRow就是一行,定义几个TableRow就是定义几行。TableLayout不会显示行或者列或者cell的边线。TableLayout的行数是由我们自己声明的,列数也是由我们间接控制的。最长的行中的每个 widget至少跨越一列,如果我们定义了三行,一行有两个widgets,一行有三个widgets,还有一个有四个,则该布局至少有四列。 我们也可以通过设置属性android:layout_span来声明一个widget跨越的列数。下面的例子标识EditView widget跨越了三列。

    <TableRow>
      <TextView android:text=”URL:” />
      <EditText
        android:id=”@+id/entry”
       android:layout_span=”3″/>
    </TableRow>

    TableLayout的每列可以根据包含的内容进行伸缩。这个是通过 TableLayout的属性android:stretchColumns来设置的,该属性的值可以设置单个列数,或者是由逗号分隔的列数组合。这些列自动扩展来占据行中可用的空间。相反的,我们可以通过设置android:shrinkColumns来word-wrap 列包含的内容,以达到压缩列的有效宽度。我们也可以通过设置android:collapseColumns 属性来控制列的可见与否,这在用户选择显示重要信息,屏蔽不重要信息的时候可以应用。可以在代码中调用setColumnStretchable() 和setColumnShrinkable()、setColumnCollapsed() 。

    注意:列数是从0开始计数的。

    <TableLayout xmlns:android=”http://schemas.android.com/apk/res/android”
        android:layout_width=”fill_parent” android:layout_height=”fill_parent”
        android:stretchColumns=”4″>
        <TableRow>
            <TextView android:layout_width=”wrap_content”
                android:layout_height=”wrap_content” android:text=”hello” />
            <EditText android:id=”@+id/entry” android:text=”world”
                android:layout_span=”3″ />
            <EditText android:text=”this is a test” android:layout_span=”2″ />
            />
        </TableRow>
        <TableRow>
            <Button android:id=”@+id/cancel” android:layout_column=”2″
                android:text=”Cancel” />
            <Button android:id=”@+id/ok” android:text=”OK”
             android:layout_column=”5″ />
        </TableRow>
    </TableLayout>

    运行效果如下:

    在看如下代码:

    <TableLayout xmlns:android=”http://schemas.android.com/apk/res/android”
        android:layout_width=”fill_parent” android:layout_height=”fill_parent”
             android:stretchColumns=”4″
             android:collapseColumns=”2″>//隐藏第三列
        <TableRow>
            <TextView android:layout_width=”wrap_content”
                android:layout_height=”wrap_content” android:text=”hello” />
            <EditText android:id=”@+id/entry” android:text=”world”
            android:layout_span=”3″ />
            <EditText android:text=”this is a test” android:layout_span=”2″ />
            />
        </TableRow>
        <TableRow>
            <Button android:id=”@+id/cancel” android:layout_column=”2″
                android:text=”Cancel” />
            <Button android:id=”@+id/ok” android:text=”OK”
                android:layout_column=”5″ />
        </TableRow>
    </TableLayout>

    4.ScrollView:提供滚动条的 Container。

    <ScrollView
      xmlns:android=”http://schemas.android.com/apk/res/android”
      android:layout_width=”fill_parent”
      android:layout_height=”wrap_content”>
      <TableLayout
        android:layout_width=”fill_parent”
        android:layout_height=”fill_parent”
        android:stretchColumns=”0″>
        <TableRow>
          <View
            android:layout_height=”80px”
            android:background=”#000000″/>
          <TextView android:text=”#000000″
            android:paddingLeft=”4px”
            android:layout_gravity=”center_vertical” />
        </TableRow>
        <TableRow>
          <View
            android:layout_height=”80px”
            android:background=”#440000″ />
          <TextView android:text=”#440000″
            android:paddingLeft=”4px”
            android:layout_gravity=”center_vertical” />
        </TableRow>
        <TableRow>
          <View
            android:layout_height=”80px”
            android:background=”#884400″ />
          <TextView android:text=”#884400″
            android:paddingLeft=”4px”
            android:layout_gravity=”center_vertical” />
        </TableRow>
        <TableRow>
          <View
            android:layout_height=”80px”
            android:background=”#aa8844″ />
          <TextView android:text=”#aa8844″
            android:paddingLeft=”4px”
            android:layout_gravity=”center_vertical” />
        </TableRow>
        <TableRow>
          <View
            android:layout_height=”80px”
            android:background=”#ffaa88″ />
          <TextView android:text=”#ffaa88″
          android:paddingLeft=”4px”
            android:layout_gravity=”center_vertical” />
        </TableRow>
        <TableRow>
          <View
            android:layout_height=”80px”
            android:background=”#ffffaa” />
          <TextView android:text=”#ffffaa”
            android:paddingLeft=”4px”
            android:layout_gravity=”center_vertical” />
        </TableRow>
        <TableRow>
          <View
            android:layout_height=”80px”
            android:background=”#ffffff” />
          <TextView android:text=”#ffffff”
            android:paddingLeft=”4px”
            android:layout_gravity=”center_vertical” />
        </TableRow>
      </TableLayout>
    </ScrollView>

    运行效果如下:

  • 相关阅读:
    【C语言】用"I love you!"打印心形
    android js与控件交互初探。
    android 调用webview控件,为逆向h5app做准备
    git命令
    kdevelp 导入makefile工程
    解决vmvare关闭过慢
    用python.twisted.logfile每天记录日志,并用不记录stdout中的内容
    关于bjam编译自己模块出错的问题
    俄罗斯黑客在美国监狱中获刑4年,到底是什么原因?
    美国的电信巨头T-Mobile今天披露了另一起数据遭黑客泄露事件
  • 原文地址:https://www.cnblogs.com/zjmsky/p/1898826.html
Copyright © 2011-2022 走看看