miniTwitter 學習

轉自:JamesLiu


先上個圖:

 

 

佈局分析:分成三個部分,該Activity是一個無標題的,設置無標題需要在setContentView之前設置,否則會報錯,

  1. requestWindowFeature(Window.FEATURE_NO_TITLE);  
  2. setContentView(R.layout.login);  

第一部分是一個帶漸變色背景的LinearLayout佈局,關於背景漸變色請參照 android小技巧:android 背景漸變色(shape,gradient) ,

這裏就不再貼上代碼了,效果如下圖所示

 

第二部分,紅色線區域內,包括1,2,3,4  如圖所示:

 

紅色的1表示的是一個帶圓角且背景色爲#55FFFFFF(淡藍色)的RelativeLayout佈局,代碼如下

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <solid android:color="#55FFFFFF" />  
  4.     <!-- 設置圓角  
  5.     注意: bottomRightRadius是左下角而不是右下角  bottomLeftRadius右下角-->  
  6.     <corners android:topLeftRadius="10dp" android:topRightRadius="10dp"  
  7.         android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/>  
  8. </shape>  

solid表示填充色,這裏填充的是淡藍色。 corners是設置圓角。

dp (即dip,device independent pixels)設備獨立像素:這個和設備硬件有關,一般我們爲了支持WVGA、HVGA和QVGA ,不依賴像素。 在android上開發的程序將會在不同分辨率的手機上運行。爲了讓程序外觀不至於相差太大,所以引入了dip的概念。比如定義一個矩形10 x 10dip. 在分辨率爲160dpi 的屏上,比如G1,正好是10 x 10像素。 而在240 dpi 的屏,則是15 x 15 像素. 換算公式爲 pixs = dips * (density/160). density 就是屏的分辨率

然後RelativeLayou的background引用此drawable,具體RelativeLayout設置如下

  1.   <RelativeLayout   
  2.     android:id="@+id/login_div"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:padding="15dip"          
  6.     android:layout_margin="15dip"   
  7.     android:background="@drawable/background_login_div_bg"  
  8.     >  
  9. </RelativeLayout>  

padding 是指內邊距(也就是指內容與邊框的距離), layout_margin爲外邊距(它的上一層與邊框的距離)。

 

接下來是區域2,爲賬號的文本和輸入框,首先是賬號的文本,代碼如下:

  1. <TextView   
  2.     android:id="@+id/login_user_input"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_alignParentTop="true"  
  6.     android:layout_marginTop="5dp"  
  7.     android:text="@string/login_label_username"  
  8.     style="@style/normalText"/>  

android:layout_alignParentTop 這裏表示此TextView的位置處於頂部

android:layout_marginTop="5dp" 這裏表示此TextView的邊框與RelativeLayout的頂部邊框距離有5dp

這裏需要對這個TextView設置下字體顏色和字體大小,定義在res/style.xml裏面

  1. <style name="normalText" parent="@android:style/TextAppearance">    
  2.        <item name="android:textColor">#444</item>  
  3.     <item name="android:textSize">14sp</item>  
  4. </style>  


定義賬號的輸入框,如下

  1. <EditText   
  2.     android:id="@+id/username_edit"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:hint="@string/login_username_hint"  
  6.     android:layout_below="@id/login_user_input"  
  7.     android:singleLine="true"  
  8.     android:inputType="text"/>  

android:hint 輸入框裏面的提示文字,

android:layout_below這裏是設置爲在賬號的文本框的下面,

android:singleLine 爲單行輸入(即你輸入回車的時候不會在換行了)

android:inputType這裏text表示 輸入的類型爲文本

 

區域3是密碼文本和輸入框,同區域2,代碼如下:

  1. <TextView   
  2.     android:id="@+id/login_password_input"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_below="@id/username_edit"  
  6.     android:layout_marginTop="3dp"  
  7.     android:text="@string/login_label_password"  
  8.     style="@style/normalText"/>  
  9. <EditText   
  10.     android:id="@+id/password_edit"  
  11.     android:layout_width="fill_parent"  
  12.     android:layout_height="wrap_content"  
  13.     android:layout_below="@id/login_password_input"  
  14.     android:password="true"  
  15.     android:singleLine="true"  
  16.     android:inputType="textPassword"  
  17. />  

 

 區域4,登錄按鈕

  1. <Button   
  2.     android:id="@+id/signin_button"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_below="@id/password_edit"  
  6.     android:layout_alignRight="@id/password_edit"  
  7.     android:text="@string/login_label_signin"  
  8.     android:background="@drawable/blue_button"  
  9.    />  

 

第三部分:底下的文字和兩張圖片,分別標記了1,2,3,4

 

區域1:還是一個RelativeLayout,但這裏設置的很簡單,代碼如下:

  1. <RelativeLayout   
  2.      android:layout_width="fill_parent"  
  3.      android:layout_height="wrap_content">  
  4. lt;/RelativeLayout>  

 

區域2:"沒有賬號?註冊"這幾個文字定義在string裏面,包含了一個<a>標籤,

  1. <string name="login_register_link">沒有帳號? <a href="#" mce_href="#">註冊</a></string>  

定義如下:

  1. <TextView  android:id="@+id/register_link"  
  2.     android:text="@string/login_register_link"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_marginLeft="15dp"  
  6.     android:textColor="#888"  
  7.     android:textColorLink="#FF0066CC"  
  8.  />  

TextView是支持簡單的html標籤的,如<a>標籤,但並不是支持所有標籤,支持更復雜的html標籤得用webView組件。

android:textColorLink是設置文字鏈接的顏色. 雖然TextView支持<a>標籤,但是這裏是不能點此鏈接的,不要被假象迷惑。

 

區域3是一直貓的卡通圖片,貌似有點醜,將就下吧,

  1. <ImageView android:id="@+id/miniTwitter_logo"  
  2.             android:src="@drawable/cat"  
  3.             android:layout_width="wrap_content"  
  4.             android:layout_height="wrap_content"  
  5.             android:layout_alignParentRight="true"  
  6.             android:layout_alignParentBottom="true"  
  7.             android:layout_marginRight="25dp"  
  8.             android:layout_marginLeft="10dp"  
  9.             android:layout_marginBottom="25dp"  
  10.              />  

android:layout_alignParentRight="true" 位於layout的最右邊

android:layout_alignParentBottom="true"  位於layout的最底部

android:layout_marginRight="25dp"  該imageView的邊框距離layout邊框有25dp,其他的margin類似。

區域4 是一個帶文字的圖片的ImageView,

  1. <ImageView android:src="@drawable/logo"  
  2.      android:layout_width="wrap_content"  
  3.      android:layout_height="wrap_content"  
  4.      android:layout_toLeftOf="@id/miniTwitter_logo"  
  5.      android:layout_alignBottom="@id/miniTwitter_logo"  
  6.      android:paddingBottom="8dp"  
  7.       />  

android:layout_toLeftOf="@id/miniTwitter_logo"  在那個小貓ImageView的左邊(水平位置)

android:layout_alignBottom="@id/miniTwitter_logo"  這裏意思是這兩個ImageView(區域3和區域4)下邊緣對齊

android:paddingBottom="8dp"  圖片距離ImageView底部邊框8dp,也就是將圖片上擡個8dp

 

 

具體步驟如下:

第一步:一些字符串定義

/miniTwitterLoginDemo/res/values/strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World, LoginActivity!</string>  
  4.     <string name="login_label_username">帳號</string>  
  5.     <string name="login_label_password">密碼</string>  
  6.     <string name="login_label_signin">登 錄</string>  
  7.     <string name="login_status_logging_in">登錄中...</string>  
  8.     <string name="login_username_hint">Email或手機號</string>  
  9.     <string name="login_register_link">沒有帳號? <a href="#" mce_href="#">註冊</a></string>  
  10.     <string name="app_name">miniTwitter</string>  
  11. </resources>  

 

第二步:

/miniTwitterLoginDemo/res/values/style.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="normalText" parent="@android:style/TextAppearance">              <item name="android:textColor">#444</item>  
  4.         <item name="android:textSize">14sp</item>  
  5.     </style>  
  6. </resources>  

 

第三步:背景色爲漸變色

/miniTwitterLoginDemo/res/drawable-mdpi/background_login.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <gradient   
  4.         android:startColor="#FFACDAE5"  
  5.         android:endColor="#FF72CAE1"  
  6.         android:angle="45"  
  7.     />  
  8. </shape>  

 

第四步:背景色味淡藍色且爲圓角

/miniTwitterLoginDemo/res/drawable-mdpi/background_login_div_bg.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <solid android:color="#55FFFFFF" />  
  4.     <!-- 設置圓角  
  5.     注意: bottomRightRadius是左下角而不是右下角  bottomLeftRadius右下角-->  
  6.     <corners android:topLeftRadius="10dp" android:topRightRadius="10dp"  
  7.         android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/>  
  8. </shape>  

 

第五步:

/miniTwitterLoginDemo/res/layout/login.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="fill_parent"  
  6.   android:layout_height="fill_parent"  
  7.   android:background="@drawable/background_login">  
  8.   <!-- padding 內邊距   layout_margin 外邊距  
  9.         android:layout_alignParentTop 佈局的位置是否處於頂部 -->  
  10.   <RelativeLayout   
  11.     android:id="@+id/login_div"  
  12.     android:layout_width="fill_parent"  
  13.     android:layout_height="wrap_content"  
  14.     android:padding="15dip"          
  15.     android:layout_margin="15dip"   
  16.     android:background="@drawable/background_login_div_bg"  
  17.     >  
  18.     <!-- 賬號 -->  
  19.     <TextView   
  20.         android:id="@+id/login_user_input"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_alignParentTop="true"  
  24.         android:layout_marginTop="5dp"  
  25.         android:text="@string/login_label_username"  
  26.         style="@style/normalText"/>  
  27.     <EditText   
  28.         android:id="@+id/username_edit"  
  29.         android:layout_width="fill_parent"  
  30.         android:layout_height="wrap_content"  
  31.         android:hint="@string/login_username_hint"  
  32.         android:layout_below="@id/login_user_input"  
  33.         android:singleLine="true"  
  34.         android:inputType="text"/>  
  35.     <!-- 密碼 text -->  
  36.     <TextView   
  37.         android:id="@+id/login_password_input"  
  38.         android:layout_width="wrap_content"  
  39.         android:layout_height="wrap_content"  
  40.         android:layout_below="@id/username_edit"  
  41.         android:layout_marginTop="3dp"  
  42.         android:text="@string/login_label_password"  
  43.         style="@style/normalText"/>  
  44.     <EditText   
  45.         android:id="@+id/password_edit"  
  46.         android:layout_width="fill_parent"  
  47.         android:layout_height="wrap_content"  
  48.         android:layout_below="@id/login_password_input"  
  49.         android:password="true"  
  50.         android:singleLine="true"  
  51.         android:inputType="textPassword"  
  52.     />  
  53.     <!-- 登錄button -->  
  54.     <Button   
  55.         android:id="@+id/signin_button"  
  56.         android:layout_width="wrap_content"  
  57.         android:layout_height="wrap_content"  
  58.         android:layout_below="@id/password_edit"  
  59.         android:layout_alignRight="@id/password_edit"  
  60.         android:text="@string/login_label_signin"  
  61.         android:background="@drawable/blue_button"  
  62.     />  
  63.   </RelativeLayout>  
  64.     
  65.     
  66.   <RelativeLayout   
  67.       android:layout_width="fill_parent"  
  68.       android:layout_height="wrap_content"  
  69.       >  
  70.      <TextView  android:id="@+id/register_link"  
  71.          android:text="@string/login_register_link"  
  72.          android:layout_width="wrap_content"  
  73.          android:layout_height="wrap_content"  
  74.          android:layout_marginLeft="15dp"  
  75.          android:textColor="#888"  
  76.          android:textColorLink="#FF0066CC"  
  77.       />  
  78.         <ImageView android:id="@+id/miniTwitter_logo"  
  79.             android:src="@drawable/cat"  
  80.             android:layout_width="wrap_content"  
  81.             android:layout_height="wrap_content"  
  82.             android:layout_alignParentRight="true"  
  83.             android:layout_alignParentBottom="true"  
  84.             android:layout_marginRight="25dp"  
  85.             android:layout_marginLeft="10dp"  
  86.             android:layout_marginBottom="25dp"  
  87.              />  
  88.      <ImageView android:src="@drawable/logo"  
  89.          android:layout_width="wrap_content"  
  90.          android:layout_height="wrap_content"  
  91.          android:layout_toLeftOf="@id/miniTwitter_logo"  
  92.          android:layout_alignBottom="@id/miniTwitter_logo"  
  93.          android:paddingBottom="8dp"  
  94.           />  
  95.         </RelativeLayout>  
  96.    
  97. </LinearLayout>  

 

 

 第七步:

/miniTwitterLoginDemo/src/com/mytwitter/acitivity/LoginActivity.java

  1. package com.mytwitter.acitivity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Window;  
  6.   
  7. public class LoginActivity extends Activity {  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  12.         setContentView(R.layout.login);  
  13.     }  
  14. }  

 

到這裏就解釋結束了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章