Android筆記(十) Android中的布局——網格布局

來源:互聯網
上載者:User

標籤:

         網格布局是Android4.0新增的布局管理器,因此需要在Android4.0之後的版本才可以使用,之前的平台使用該布局的話,需要匯入相應的支援庫。

         GridLayout的作用類似於HTML中的table標籤,它把整個容器劃分成row*column個網格,每個網格都可以放置一個組件,也可以設定一個組件橫跨多少列、多少行。

         GridLayout提供了setRowCount(int)和setColumnCount(int)方法來控制該網格的行數和列數。

         簡單程式碼範例:

         gridlayout.xml

<?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:rowCount="6"    android:columnCount="4"    android:id="@+id/root">    <!-- 定一個一個橫跨四列的文字框,並設定該文字框的前景色彩、背景色等屬性 -->    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_columnSpan="4"        android:textSize="50sp"        android:layout_marginLeft="4px"        android:layout_marginRight="4px"        android:padding="5px"        android:layout_gravity="right"        android:background="#eee"        android:textColor="#000"        android:text="0"        />    <!-- 定義一個橫跨四列的按鈕 -->    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_columnSpan="4"        android:text="清除"/>    <!-- 添加其他按鈕 -->    <Button        android:text="7"        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="8"        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="9"        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="/"        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="4"        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="5"        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="6"        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="*"        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="1"        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="2"        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="3"        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="-"        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="0"        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="."        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="+"        android:layout_rowWeight="1"        android:layout_columnWeight="1"/>    <Button        android:text="="        android:layout_rowWeight="1"        android:layout_columnWeight="1"/></GridLayout>

  運行結果:

 

Android筆記(十) Android中的布局——網格布局

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.