Android UI組件----用相對布局RelativeLayout做一個登陸界面


【聲明】

歡迎轉載,但請保留文章原始出處→_→

生命壹號:http://www.cnblogs.com/smyhvae/

文章來源:http://www.cnblogs.com/smyhvae/p/3839986.html

 

【正文】

兩個小時的學習成果,對於我這種還沒入門但渴望不斷進步的初學者來說,是一種激勵。通過自己一行一行的敲代碼,實現了用相對布局做一個登陸界面的效果。

XML文件完整版代碼如下:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2  xmlns:tools="http://schemas.android.com/tools"
 3  android:layout_width="match_parent"
 4  android:layout_height="match_parent"
 5  android:padding="20dp" >
 6 
 7     <!--定義"登陸界面"四個字的文本框,放在最上方的中央 -->
 8     <TextView  9         android:id="@+id/lableView"
10  android:layout_width="wrap_content"
11  android:layout_height="wrap_content"
12  android:layout_centerHorizontal="true"
13  android:text="登陸界面" />
14     
15     <!--定義輸入用戶名的文本編輯框 -->
16     <EditText 17         android:id="@+id/usernameView"
18  android:layout_width="wrap_content"
19  android:layout_height="wrap_content"        
20  android:layout_below="@id/lableView"
21  android:layout_alignParentLeft="true"
22  android:layout_alignParentRight="true"
23  android:hint="username"    />
24     
25     <!--定義輸入密碼的文本編輯框 -->
26     <EditText 27         android:id="@+id/passwordView"
28  android:layout_width="wrap_content"
29  android:layout_height="wrap_content"        
30  android:layout_below="@id/usernameView"
31  android:layout_alignParentLeft="true"
32  android:layout_alignParentRight="true"
33  android:hint="password"
34  android:inputType="textPassword" />
35     
36     <!--定義“取消”的按鈕 -->
37     <Button 38         android:id="@+id/cancleButton"
39  android:layout_width="wrap_content"
40  android:layout_height="wrap_content"
41  android:layout_below="@id/passwordView"
42  android:layout_alignParentRight="true"
43  android:text="取消" />
44     
45     <!--定義“確定”的按鈕,放在“取消”按鈕的左邊 -->
46     <Button 47         android:id="@+id/okButton"
48  android:layout_width="wrap_content"
49  android:layout_height="wrap_content"
50  android:layout_alignBottom="@id/cancleButton"
51  android:layout_toLeftOf="@id/cancleButton"
52  android:text="確定" />        
53 
54 </RelativeLayout>

 

代碼解釋如下:

05行:設置整個RelativeLayout的內邊距為20dp,給界面留白。
21、22行:讓編輯框的左邊對齊到父控件的左邊,右邊對齊到父控件的右邊,這樣一來,就剛好橫向覆蓋整個界面。
30行:將passwordView放到usernameView的下面,以保證二者的相對位置不變。
34行:輸入密碼時顯示為星號,以保證密碼的安全
41、42行:將“取消”按鈕放到passwordView的下面,並貼齊到父控件的右邊
50、51行:將“確定”按鈕放到“取消”按鈕的左邊,並貼齊到“取消”按鈕的下邊,以保證二者的相對位置不變

最終在手機上運行后,顯示效果如下:

 

 

 

我的公眾號

想學習代碼之外的軟技能?不妨關注我的微信公眾號:生命團隊(id:vitateam)。

掃一掃,你將發現另一個全新的世界,而這將是一場美麗的意外:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM