用GridLayout實現計算機的布局

來源:互聯網
上載者:User

標籤:android   style   blog   http   java   color   

GridLayout是一個表格視圖,我們一般定義它的列數和行數來設定好這個控制項。

下面的布局檔案先定義了一個TextView和一個Button,設定他們的屬性為橫跨4列。這樣就可以獨自佔據一行了。然後我們用代碼來放入其他的子項目,其實就是些Button。

布局檔案

<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"    tools:context="${relativePackage}.${activityClass}"      xmlns:app="http://schemas.android.com/apk/res/com.kale.gridlayout">    <!-- 六行四列 -->    <android.support.v7.widget.GridLayout        android:id="@+id/root_gridLayout_id"        android:layout_width="match_parent"        android:layout_height="match_parent"         app:rowCount="6"        app:columnCount="4">        <TextView            android:layout_width="match_parent"            android:layout_height="100dp"            app:layout_columnSpan="4"            android:text="Large Text"            android:textAppearance="?android:attr/textAppearanceLarge" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            app:layout_gravity="center_horizontal|top"            app:layout_columnSpan="4"            android:text="Button" />    </android.support.v7.widget.GridLayout></RelativeLayout>

 

MainActivity.java

package com.kale.gridlayout;import android.app.Activity;import android.os.Bundle;import android.support.v7.widget.GridLayout;import android.view.Gravity;import android.widget.Button;public class MainActivity extends Activity {    String [] chars = new String[]{        "7","8","9","÷",        "4","5","6","x",        "1","2","3","-",        ".","0","=","+"    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                GridLayout gLayout = (GridLayout)findViewById(R.id.root_gridLayout_id);        for (int i = 0; i < chars.length; i++) {            Button bt = new Button(this);            bt.setText(chars[i]);            bt.setTextSize(40);                        //指定該組件所佔的行            GridLayout.Spec rowSpec = GridLayout.spec(i/4 + 2);            //指定該組件所佔的列            GridLayout.Spec columnSpec = GridLayout.spec(i % 4);             //按照設定好的行和列來建立對象            GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec,columnSpec);            //指定該組件佔滿父控制項            params.setGravity(Gravity.FILL);            gLayout.addView(bt,params);        }            }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.