国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發中include控件用法分析

2019-12-12 04:59:06
字體:
來源:轉載
供稿:網友

本文實例講述了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的布局,沒有相應的布局,結果是非常慘的,大家可以根據我的代碼在修改下相應的布局,體會下main中布局的重要性!

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

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万州区| 什邡市| 潞城市| 壤塘县| 馆陶县| 印江| 财经| 阳谷县| 海兴县| 汉寿县| 神池县| 睢宁县| 田东县| 奎屯市| 静宁县| 正定县| 南投市| 辉南县| 阜康市| 罗山县| 图们市| 珲春市| 三亚市| 托克托县| 恩平市| 二连浩特市| 绥德县| 鄂托克前旗| 济宁市| 县级市| 文水县| 龙泉市| 孟津县| 平邑县| 华宁县| 灌南县| 新宾| 怀远县| 罗定市| 乃东县| 西乡县|