Android開發中include控制項用法分析_Android

來源:互聯網
上載者:User

本文執行個體講述了Android開發中include控制項用法。分享給大家供大家參考,具體如下:

我們知道,基於Android系統的應用程式的開發,介面設計是非常重要的,它關係著使用者體驗的好壞。一個好的介面設計,不是用一個xml布局就可以搞定的。當一個activity中的控制項非常多的時候,所有的布局檔案都放在一個xml檔案中,很容易想象那是多麼糟糕的事情!筆者通過自身的經曆,用include控制項來解決這個問題,下面是一個小例子,僅僅實現的是布局,沒有響應代碼的設計。

user.xml檔案內容如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="horizontal" >  <TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="使用者名稱: " />  <EditText    android:layout_width="150dp"    android:layout_height="wrap_content"    android:id="@+id/userName"    android:hint="請輸入使用者名稱"    /></LinearLayout>

passwd.xml檔案內容如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="horizontal" >   <TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="密   碼:" />  <EditText    android:layout_width="150dp"    android:layout_height="wrap_content"    android:id="@+id/passWd"    android:hint="請輸入密碼"    /></LinearLayout>

login.xml檔案內容如下:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="horizontal" >  <Button    android:layout_width="80dp"    android:layout_height="wrap_content"    android:id="@+id/bt"    android:hint="確定"    />  <Button    android:layout_width="80dp"    android:layout_height="wrap_content"    android:id="@+id/reset"    android:layout_toRightOf="@id/bt"    android:hint="重設"    /></RelativeLayout>

main.xml檔案內容如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical"  android:layout_alignParentBottom="true"><RelativeLayout  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical"  android:layout_alignParentBottom="true"> <LinearLayout  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:id="@+id/head"  android:layout_alignParentTop="true">   <include   android:layout_width="fill_parent"   layout="@layout/user">   </include> </LinearLayout> <LinearLayout  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:id="@+id/middle"  android:layout_below="@id/head"  android:layout_alignParentLeft="true">   <include   android:layout_width="fill_parent"   layout="@layout/passwd">   </include>  </LinearLayout>  <LinearLayout  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:id="@+id/foot"  android:layout_below="@id/middle"  android:layout_alignParentRight="true">   <include   android:layout_width="fill_parent"   layout="@layout/login">   </include>   </LinearLayout></RelativeLayout></LinearLayout>

程式運行結果如下:

如果在main.xml中這樣寫:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical"  android:layout_alignParentBottom="true">   <include   android:layout_width="fill_parent"   layout="@layout/user">   </include>   <include   android:layout_width="fill_parent"   layout="@layout/passwd">   </include>   <include   android:layout_width="fill_parent"   layout="@layout/login">   </include></LinearLayout>

那麼該情況下的運行結果如下:

很顯然運行結果與預期不符,接下來的四個控制項出不來,為什麼呢?(想必大家在做實驗的時候,肯定遇到過這個問題!)

其實關鍵的地方是main檔案中對各個xml的布局,沒有相應的布局,結果是非常慘的,大家可以根據My Code在修改下相應的布局,體會下main中布局的重要性!

更多關於Android相關內容感興趣的讀者可查看本站專題:《Android視圖View技巧總結》、《Android圖形與影像處理技巧總結》、《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方案匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》、《Android布局layout技巧總結》及《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.