首页 > 代码库 > Android入门系列:UI组件:布局管理器:GridLayout

Android入门系列:UI组件:布局管理器:GridLayout

写一个计算器的示例,布局文件如下:

<?xml version="1.0" encoding="utf-8"?><GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_gravity="center"    android:useDefaultMargins="true"    android:columnCount="4"    >        <Button         android:text="@string/zero"        android:background="#DFDFDF"        android:textSize="30sp"        android:layout_columnSpan="4"        android:gravity="center_vertical|right"        android:paddingRight="8dp"        android:layout_gravity="fill"        android:clickable="false"        />        <Button         android:text="@string/clear"        android:layout_columnSpan="3"        android:layout_gravity="fill"        />    <Button        android:text="@string/divide"        android:layout_gravity="fill"        />    <Button         android:text="@string/one"        android:layout_gravity="fill"        />    <Button         android:text="@string/two"        android:layout_gravity="fill"        />    <Button         android:text="@string/three"        android:layout_gravity="fill"        />    <Button         android:text="@string/multiply"        android:layout_gravity="fill"        />    <Button         android:text="@string/four"        android:layout_gravity="fill"        />    <Button         android:text="@string/five"        android:layout_gravity="fill"        />    <Button         android:text="@string/six"        android:layout_gravity="fill"        />    <Button         android:text="@string/sub"        android:layout_gravity="fill"        />    <Button         android:text="@string/seven"        android:layout_gravity="fill"        />    <Button         android:text="@string/eight"        android:layout_gravity="fill"        />    <Button         android:text="@string/nine"        android:layout_gravity="fill"        />    <Button         android:text="@string/add"        android:layout_rowSpan="3"        android:layout_gravity="fill"        />    <Button         android:text="@string/zero"        android:layout_columnSpan="2"        android:layout_gravity="fill"        />    <Button         android:text="@string/dot"        android:layout_gravity="fill"        />                       <Button         android:text="@string/equal"        android:layout_columnSpan="3"        android:layout_gravity="fill"        />        </GridLayout>

其中,字符串文件为res/values/string.xml:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">My Second App</string>    <string name="hello_world">Hello world!</string>    <string name="action_settings">Settings</string>    <string name="str_hello">Hello,安卓!</string>    <string name="calc_title">计算器</string>    <string name="zero">0</string>    <string name="one">1</string>    <string name="two">2</string>    <string name="three">3</string>    <string name="four">4</string>    <string name="five">5</string>    <string name="six">6</string>    <string name="seven">7</string>    <string name="eight">8</string>    <string name="nine">9</string>    <string name="dot">.</string>    <string name="add">+</string>    <string name="sub">-</string>    <string name="multiply">*</string>    <string name="divide">/</string>    <string name="equal">=</string>    <string name="clear">C</string></resources>

运行的结果为:

Screenshot_2014-12-05-15-20-48

 

 

显示的结果最后一行(第7行)和最后一列(第4列)不是想象中的样子。

Android入门系列:UI组件:布局管理器:GridLayout