Android (介面編程#7-可視化編程)

來源:互聯網
上載者:User
  布局XML檔案使用:     
      就如跨平台UI介面庫一樣,Android也是使用XML檔案來存貯界元素持布局,現在流行的一些介面組件都是採用此方式。
在Android中,res/layout資來源目錄下,會有一個或多個.xml檔案,這就是一個介面的布局檔案。我們開啟一個來看看。我開啟當前工程目錄下的res/layout/main.xml檔案。

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    android:id="@+id/mainview"
    />

</LinearLayout>

這個檔案很簡單的布局是指用了一個LinearLayout來布局,裡面只有一個TextView介面元素,也就是一個View.當Activity載入View時,就可以在onCreate中直接載入。this.setContentView(R.layout.main);其中R.layout.main就是一個素引值,是由android開發環境編譯產生的,是映射到res/layout/main.xml的。
所以setContentView(R.layout.main);等價於,按裝main.xml的布局來配置一個layout.然後載入,
與如下代碼效果一致
LinearLayout layout = new LinearLayout(this);
TextView tv = new TextView(this);
tv.setText(R.string.hello);
layout.addView(tv);
this.setContentView(layout);
其中R.string.hello也是一個資源地圖的ID,是指載入res/values/string.xml中的hello對應的值。
 上述例子是一個簡單的例子,知道了如何載入布局那如何產生,配置這樣的layout檔案呢?

可視化編程:
建立一個項目,步聚就如Hello,World例子中一樣,
然後找到左邊的res/layout/main.xml雙擊開啟,Eclipse右邊會 自動開啟這個xml檔案的layout視圖,視圖周圍有一些操作這個layout的工具列,拖拉一些你想要的
控制項到這個視圖上,那麼xml檔案就會被改變,在最右邊的 Outline視圖中,有這個布局檔案中介面元素的樹狀結構圖。
玩這個東東,想必都會玩,與可視化的VS,Delphi,Swing,QT,沒有任何區別,那我們再來一個小例子吧:

相關文章

聯繫我們

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