Android 開發:第二日——第一次使用控制項

來源:互聯網
上載者:User

  先說說怎麼建立一個Activity:

/** 建立Activity的要點* 1.一個Activity就是一個類,這個類需要繼承於Activity* 2.需要重寫onCreate方法* 3.每一個Activity都需要在AndroidMainifest.xml檔案當中進行配置* 4.為Activity添加必要的控制項* */ 

 

  其中第4步,為這個Activity建立一個Button控制項和一個TextView控制項。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"               android:orientation="vertical"               android:layout_width="fill_parent"               android:layout_height="fill_parent">    <TextView        android:id="@+id/myTextView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>        <Button         android:id="@+id/myButton"        android:layout_width="fill_parent"        android:layout_height="wrap_content"/></LinearLayout>

 

  同時把建立工程的相對布局(RelativeLayout)修改為相對比較簡單的線性布局(LinearLayout)。

  補充一下,Android共有5種布局方式:

FrameLayout(架構布局),LinearLayout (線性布局),AbsoluteLayout(絕對布局),RelativeLayout(相對布局),TableLayout(表格版面配置)

  回到Java代碼,在onCreate()方法中:

@Override    public void onCreate(Bundle savedInstanceState) {        // 調用父類中的onCreate()方法        super.onCreate(savedInstanceState);        // 設定當前Activity所使用的布局檔案        setContentView(R.layout.activity_main);        // 下面函數的傳回值是View,View是所有Android控制項的父類        TextView myTextView = (TextView)findViewById(R.id.myTextView);        Button myButton = (Button)findViewById(R.id.myButton);        myTextView.setText("第一個TextView");        myButton.setText("第一個Button");    }

 

 

注意在檔案頭部引用:

import android.widget.Button;import android.widget.TextView;

 

OK,運行效果:

每天努力一點點,加油!

------------------------------------------------------------------------------------------

作者:龐輝

出處:http://www.cnblogs.com/pang123hui/

本文基於署名 2.5 中國大陸許可協議發布,歡迎轉載,演繹或用於商業目的,但是必須保留本文的署名龐輝(包含連結).

相關文章

聯繫我們

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