Android miniTwitter登錄界面開(kāi)發(fā)實(shí)例
本文要演示的Android開(kāi)發(fā)實(shí)例是如何完成一個(gè)Android中的miniTwitter登錄界面,下面將分步驟講解怎樣實(shí)現(xiàn)圖中的界面效果,讓大家都能輕松的做出美觀(guān)的登錄界面。
先貼上最終要完成的效果圖:
miniTwitter登錄界面的布局分析
首先由界面圖分析布局,基本可以分為三個(gè)部分,下面分別講解每個(gè)部分。
第一部分是一個(gè)帶漸變色背景的LinearLayout布局,關(guān)于背景漸變色就不再貼代碼了,效果如下圖所示:
第二部分,紅色線(xiàn)區(qū)域內(nèi),包括1,2,3,4 如圖所示:
紅色的1表示的是一個(gè)帶圓角且背景色為#55FFFFFF(淡藍(lán)色)的RelativeLayout布局,代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#55FFFFFF" /> <!-- 設(shè)置圓角 注意: bottomRightRadius是左下角而不是右下角 bottomLeftRadius右下角--> <corners android:topLeftRadius="10dp" android:topRightRadius="10dp" android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/> </shape>
solid表示填充色,這里填充的是淡藍(lán)色。corners是設(shè)置圓角。
dp (即dip,device independent pixels)設(shè)備獨(dú)立像素:這個(gè)和設(shè)備硬件有關(guān),一般我們?yōu)榱酥С諻VGA、HVGA和QVGA ,不依賴(lài)像素。在android上開(kāi)發(fā)的程序?qū)?huì)在不同分辨率的手機(jī)上運(yùn)行。為了讓程序外觀(guān)不至于相差太大,所以引入了dip的概念。比如定義一個(gè)矩形10 x 10dip. 在分辨率為160dpi 的屏上,比如G1,正好是10 x 10像素。 而在240 dpi 的屏,則是15 x 15 像素。換算公式為 pixs = dips * (density/160)。density 就是屏的分辨率。
然后RelativeLayou的background引用此drawable,具體RelativeLayout設(shè)置如下:
<RelativeLayout android:id="@+id/login_div" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="15dip" android:layout_margin="15dip" android:background="@drawable/background_login_div_bg" > </RelativeLayout>
padding 是指內(nèi)邊距(也就是指內(nèi)容與邊框的距離),layout_margin為外邊距(它的上一層與邊框的距離)。
接下來(lái)是區(qū)域2,為賬號(hào)的文本和輸入框,首先是賬號(hào)的文本,代碼如下:
<TextView android:id="@+id/login_user_input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="5dp" android:text="@string/login_label_username" style="@style/normalText"/>
android:layout_alignParentTop 這里表示此TextView的位置處于頂部
android:layout_marginTop="5dp" 這里表示此TextView的邊框與RelativeLayout的頂部邊框距離有5dp
這里需要對(duì)這個(gè)TextView設(shè)置下字體顏色和字體大小,定義在res/style.xml里面:
<style name="normalText" parent="@android:style/TextAppearance"> <item name="android:textColor">#444</item> <item name="android:textSize">14sp</item> </style>
定義賬號(hào)的輸入框,如下:
<EditText android:id="@+id/username_edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/login_username_hint" android:layout_below="@id/login_user_input" android:singleLine="true" android:inputType="text"/>
android:hint 輸入框里面的提示文字,android:layout_below這里是設(shè)置為在賬號(hào)的文本框的下面,android:singleLine 為單行輸入(即你輸入回車(chē)的時(shí)候不會(huì)在換行了),android:inputType這里text表示輸入的類(lèi)型為文本。
區(qū)域3是密碼文本和輸入框,同區(qū)域2,代碼如下:
<TextView android:id="@+id/login_password_input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/username_edit" android:layout_marginTop="3dp" android:text="@string/login_label_password" style="@style/normalText"/> <EditText android:id="@+id/password_edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/login_password_input" android:password="true" android:singleLine="true" android:inputType="textPassword" />
區(qū)域4,登錄按鈕:
<Button android:id="@+id/signin_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/password_edit" android:layout_alignRight="@id/password_edit" android:text="@string/login_label_signin" android:background="@drawable/blue_button" />
第三部分:底下的文字和兩張圖片,分別標(biāo)記了1,2,3,4:
Android miniTwitter登錄界面
區(qū)域1:還是一個(gè)RelativeLayout,但這里設(shè)置的很簡(jiǎn)單,代碼如下:
<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> </RelativeLayout>
區(qū)域2:"沒(méi)有賬號(hào)?注冊(cè)"這幾個(gè)文字定義在string里面,包含了一個(gè)<a>標(biāo)簽:
<string name="login_register_link">沒(méi)有帳號(hào)? <a href="#" mce_href="#">注冊(cè)</a></string>
定義如下:
<TextView android:id="@+id/register_link" android:text="@string/login_register_link" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:textColor="#888" android:textColorLink="#FF0066CC" />
TextView是支持簡(jiǎn)單的html標(biāo)簽的,如<a>標(biāo)簽,但并不是支持所有標(biāo)簽,支持更復(fù)雜的html標(biāo)簽得用webView組件。
android:textColorLink是設(shè)置文字鏈接的顏色. 雖然TextView支持<a>標(biāo)簽,但是這里是不能點(diǎn)此鏈接的,不要被假象迷惑。
區(qū)域3是一直貓的卡通圖片,貌似有點(diǎn)丑,將就下吧:
XML/HTML代碼
<ImageView android:id="@+id/miniTwitter_logo" android:src="@drawable/cat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_marginRight="25dp" android:layout_marginLeft="10dp" android:layout_marginBottom="25dp" />
android:layout_alignParentRight="true" 位于layout的最右邊
android:layout_alignParentBottom="true" 位于layout的最底部
android:layout_marginRight="25dp" 該imageView的邊框距離layout邊框有25dp,其他的margin類(lèi)似。
區(qū)域4 是一個(gè)帶文字的圖片的ImageView:
<ImageView android:src="@drawable/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/miniTwitter_logo" android:layout_alignBottom="@id/miniTwitter_logo" android:paddingBottom="8dp" /> android:layout_toLeftOf="@id/miniTwitter_logo" 在那個(gè)小貓ImageView的左邊(水平位置) android:layout_alignBottom="@id/miniTwitter_logo" 這里意思是這兩個(gè)ImageView(區(qū)域3和區(qū)域4)下邊緣對(duì)齊 android:paddingBottom="8dp" 圖片距離ImageView底部邊框8dp,也就是將圖片上抬個(gè)8dp
實(shí)現(xiàn)miniTwitter登陸界面的具體步驟
具體步驟如下:
第一步:一些字符串定義
/miniTwitterLoginDemo/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, LoginActivity!</string> <string name="login_label_username">帳號(hào)</string> <string name="login_label_password">密碼</string> <string name="login_label_signin">登 錄</string> <string name="login_status_logging_in">登錄中...</string> <string name="login_username_hint">Email或手機(jī)號(hào)</string> <string name="login_register_link">沒(méi)有帳號(hào)? <a href="#" mce_href="#">注冊(cè)</a></string> <string name="app_name">miniTwitter</string> </resources>
第二步:
/miniTwitterLoginDemo/res/values/style.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="normalText" parent="@android:style/TextAppearance"> <item name="android:textColor">#444</item> <item name="android:textSize">14sp</item> </style> </resources>
第三步:背景色為漸變色
/miniTwitterLoginDemo/res/drawable-mdpi/background_login.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="#FFACDAE5" android:endColor="#FF72CAE1" android:angle="45" /> </shape>
第四步:背景色味淡藍(lán)色且為圓角
/miniTwitterLoginDemo/res/drawable-mdpi/background_login_div_bg.xml
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#55FFFFFF" /> <!-- 設(shè)置圓角 注意: bottomRightRadius是左下角而不是右下角 bottomLeftRadius右下角--> <corners android:topLeftRadius="10dp" android:topRightRadius="10dp" android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/> </shape>
第五步:
/miniTwitterLoginDemo/res/layout/login.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" android:background="@drawable/background_login"> <!-- padding 內(nèi)邊距 layout_margin 外邊距 android:layout_alignParentTop 布局的位置是否處于頂部 --> <RelativeLayout android:id="@+id/login_div" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="15dip" android:layout_margin="15dip" android:background="@drawable/background_login_div_bg" > <!-- 賬號(hào) --> <TextView android:id="@+id/login_user_input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="5dp" android:text="@string/login_label_username" style="@style/normalText"/> <EditText android:id="@+id/username_edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/login_username_hint" android:layout_below="@id/login_user_input" android:singleLine="true" android:inputType="text"/> <!-- 密碼 text --> <TextView android:id="@+id/login_password_input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/username_edit" android:layout_marginTop="3dp" android:text="@string/login_label_password" style="@style/normalText"/> <EditText android:id="@+id/password_edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/login_password_input" android:password="true" android:singleLine="true" android:inputType="textPassword" /> <!-- 登錄button --> <Button android:id="@+id/signin_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/password_edit" android:layout_alignRight="@id/password_edit" android:text="@string/login_label_signin" android:background="@drawable/blue_button" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/register_link" android:text="@string/login_register_link" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:textColor="#888" android:textColorLink="#FF0066CC" /> <ImageView android:id="@+id/miniTwitter_logo" android:src="@drawable/cat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_marginRight="25dp" android:layout_marginLeft="10dp" android:layout_marginBottom="25dp" /> <ImageView android:src="@drawable/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/miniTwitter_logo" android:layout_alignBottom="@id/miniTwitter_logo" android:paddingBottom="8dp" /> </RelativeLayout> </LinearLayout>
第七步:
/miniTwitterLoginDemo/src/com/mytwitter/acitivity/LoginActivity.java
這里要注意的是,該Activity是無(wú)標(biāo)題的,設(shè)置無(wú)標(biāo)題需要在setContentView之前設(shè)置,否則會(huì)報(bào)錯(cuò)。
package com.mytwitter.acitivity; import android.app.Activity; import android.os.Bundle; import android.view.Window; public class LoginActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.login); } }
到此,Android中的miniTwitter登錄界面的制作就介紹完畢了,是不是做出不錯(cuò)的登錄界面并不算難呢?謝謝大家
的閱讀。
相關(guān)文章
Android實(shí)現(xiàn)網(wǎng)絡(luò)多線(xiàn)程斷點(diǎn)續(xù)傳下載實(shí)例
本示例介紹在A(yíng)ndroid平臺(tái)下通過(guò)HTTP協(xié)議實(shí)現(xiàn)斷點(diǎn)續(xù)傳下載。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10Android ListView彈性效果的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了Android ListView彈性效果的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-05-05Flutter彈性布局Flex水平排列Row垂直排列Column使用示例
這篇文章主要為大家介紹了Flutter彈性布局Flex水平排列Row垂直排列Column使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Android第三方開(kāi)源下拉框NiceSpinner使用詳解
這篇文章主要為大家詳細(xì)介紹了Android第三方開(kāi)源下拉框NiceSpinner的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android控件之ProgressBar用法實(shí)例分析
這篇文章主要介紹了Android控件之ProgressBar用法,以一個(gè)完整實(shí)例形式較為詳細(xì)的分析了ProgressBar控件操作進(jìn)度顯示的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09android 中 SQLiteOpenHelper的封裝使用詳解
這篇文章主要介紹了android 中 SQLiteOpenHelper的封裝使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02android 無(wú)須root截圖方案的實(shí)現(xiàn)
這篇文章主要介紹了android 無(wú)須root截圖方案的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03AndroidStudio 3.6 中 R.layout 找不到對(duì)應(yīng)的xml文件問(wèn)題及解決方法
這篇文章主要介紹了AndroidStudio 3.6 中 R.layout 找不到對(duì)應(yīng)的xml文件問(wèn)題,本文給出了解決方法對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03