Android開發(fā)中include控件用法分析
本文實(shí)例講述了Android開發(fā)中include控件用法。分享給大家供大家參考,具體如下:
我們知道,基于Android系統(tǒng)的應(yīng)用程序的開發(fā),界面設(shè)計是非常重要的,它關(guān)系著用戶體驗(yàn)的好壞。一個好的界面設(shè)計,不是用一個xml布局就可以搞定的。當(dāng)一個activity中的控件非常多的時候,所有的布局文件都放在一個xml文件中,很容易想象那是多么糟糕的事情!筆者通過自身的經(jīng)歷,用include控件來解決這個問題,下面是一個小例子,僅僅實(shí)現(xiàn)的是布局,沒有響應(yīng)代碼的設(shè)計。
user.xml文件內(nèi)容如下:
<?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文件內(nèi)容如下:
<?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文件內(nèi)容如下:
<?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文件內(nèi)容如下:
<?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>
程序運(yùn)行結(jié)果如下:
如果在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>
那么該情況下的運(yùn)行結(jié)果如下:
很顯然運(yùn)行結(jié)果與預(yù)期不符,接下來的四個控件出不來,為什么呢?(想必大家在做實(shí)驗(yàn)的時候,肯定遇到過這個問題?。?/p>
其實(shí)關(guān)鍵的地方是main文件中對各個xml的布局,沒有相應(yīng)的布局,結(jié)果是非常慘的,大家可以根據(jù)我的代碼在修改下相應(yīng)的布局,體會下main中布局的重要性!
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結(jié)》、《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android開發(fā)之merge結(jié)合include優(yōu)化布局
- Android編程之include文件的使用方法
- android使用include調(diào)用內(nèi)部組件的方法
- Android中使用include標(biāo)簽和merge標(biāo)簽重復(fù)使用布局
- 解析android中include標(biāo)簽的使用
- Android控件系列之TextView使用介紹
- Android控件之ImageView用法實(shí)例分析
- Android控件ListView用法(讀取聯(lián)系人示例代碼)
- Android AutoCompleteTextView控件使用實(shí)例
- Android控件之EditView常用屬性及應(yīng)用方法
相關(guān)文章
Android中imageView圖片放大縮小及旋轉(zhuǎn)功能示例代碼
這篇文章主要介紹了Android中imageView圖片放大縮小及旋轉(zhuǎn)功能示例代碼,需要的朋友可以參考下2017-08-08Android判斷屏幕是橫屏或是豎屏的簡單實(shí)現(xiàn)方法
這篇文章主要介紹了Android判斷屏幕是橫屏或是豎屏的簡單實(shí)現(xiàn)方法,是Android應(yīng)用開發(fā)中常用的功能,需要的朋友可以參考下2014-07-07仿墨跡天氣在Android App中實(shí)現(xiàn)自定義zip皮膚更換
這篇文章主要介紹了仿墨跡天氣在Android App中實(shí)現(xiàn)自定義zip皮膚更換的方法,即讓用戶可以自行通過自制或者下載的zip皮膚包進(jìn)行換膚,需要的朋友可以參考下2016-02-02Android自定義FloatingText仿點(diǎn)贊+1特效
這篇文章主要為大家詳細(xì)介紹了Android自定義FloatingText仿點(diǎn)贊+1特效,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03