先上效果图:
布局分析:分成三个部分,该Activity是一个无标题的,设置无标题需要在setContentView之前设置,否则会报错: requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.login);
第一部分是一个带渐变色背景的LinearLayout布局,关于背景渐变色请参照android小技巧:android背景渐变色(shape,gradient), 这里就不再贴上代码了,效果如下图所示
第二部分,红色线区域内,包括1,2,3,4如图所示:
红色的1表示的是一个带圆角且背景色为#55FFFFFF(淡蓝色)的RelativeLayout布局,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas./apk/res/android"> <solid android:color="#55FFFFFF" /> <!-- 设置圆角 注意: bottomRightRadius是左下角而不是右下角 bottomLeftRadius右下角--> <corners android:topLeftRadius="10dp" android:topRightRadius="10dp" android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/> </shape>
solid表示填充色,这里填充的是淡蓝色。corners是设置圆角。 dp(即dip,deviceindependentpixels)设备独立像素:这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA,不依赖像素。在android上开发的程序将会在不同分辨率的手机上运行。为了让程序外观不至于相差太大,所以引入了dip的概念。比如定义一个矩形10x10dip.在分辨率为160dpi的屏上,比如G1,正好是10x10像素。而在240dpi的屏,则是15x15像素.换算公式为pixs=dips*(density/160).density就是屏的分辨率。
然后RelativeLayou的background引用此drawable,具体RelativeLayout设置如下:
<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是指内边距(也就是指内容与边框的距离),layout_margin为外边距(它的上一层与边框的距离)。
接下来是区域2,为账号的文本和输入框,首先是账号的文本,代码如下: <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 这里需要对这个TextView设置下字体颜色和字体大小,定义在res/style.xml里面
<style name="normalText" parent="@android:style/TextAppearance"> <item name="android:textColor">#444</item> <item name="android:textSize">14sp</item> </style>
定义账号的输入框,如下 <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这里是设置为在账号的文本框的下面, android:singleLine为单行输入(即你输入回车的时候不会在换行了) android:inputType这里text表示输入的类型为文本
区域3是密码文本和输入框,同区域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" />
区域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" />
第三部分:底下的文字和两张图片,分别标记了1,2,3,4
区域1:还是一个RelativeLayout,但这里设置的很简单,代码如下: <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> </RelativeLayout>
区域2:"没有账号?注册"这几个文字定义在string里面,包含了一个<a>标签, <string name="login_register_link">没有账号? <a href="#" mce_href="#">注册</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是支持简单的html标签的,如<a>标签,但并不是支持所有标签,支持更复杂的html标签得用webView组件。 android:textColorLink是设置文字联机的颜色,虽然TextView支持<a>标签,但是这里不能点击此链接,不要被假象所迷惑。 区域3是一直猫的卡通图片,貌似有点丑,将就下吧, android:layout_alignParentRight="true"位于layout的最右边 android:layout_alignParentBottom="true"位于layout的最底部 android:layout_marginRight="25dp"该imageView的边框距离layout边框有25dp,其他的margin类似。
区域4是一个带文字的图片的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"在那个小猫ImageView的左边(水平位置) android:layout_alignBottom="@id/miniTwitter_logo"这里意思是这两个ImageView(区域3和区域4)下边缘对齐 android:paddingBottom="8dp"图片距离ImageView底部边框8dp,也就是将图片上抬个8dp
|