Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

打造精緻的 Android 登入介面:漸層背景與圓角卡片實作全解析

編輯:關於 Android 程式設計

在網路上無意間看到一個相當別緻的登入介面,這裡特別整理出來與大家分享。先來欣賞一下完成後的效果:

這個 Demo 相當輕巧,除了按鈕、小貓與 Logo 使用了圖片素材之外,其餘的視覺元素幾乎全靠程式碼打造,非常適合作為入門練習。

一、漸層背景的製作

畫面那抹柔和的藍色漸層,其實是透過一個 XML 檔來定義的,做法如下:

background_login.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#FFACDAE5"
        android:endColor="#FF72CAE1"
        android:angle="45"
    />
</shape>

其中 startColor 代表漸層的起始色,endColor 是結束色,angle 則決定漸層傾斜的角度。以 #FFACDAE5 為例,前兩碼 FF 是 Alpha 透明度,接著的 AC、DA、E5 分別對應 RGB 的紅、綠、藍,每個數值範圍都是 00 到 FF,也就是 0 到 255。若想調出特定色碼,可以直接用 PS 的取色器查詢。

二、半透明圓角卡片

效果圖上方那塊看起來像白框的區域,其實底色是白色,只是加上了透明度,同樣靠一個 XML 檔就能完成。

background_login_div.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <solid android:color="#55FFFFFF" />
    <!-- 設定圓角,注意 bottomRightRadius 其實是左下角,bottomLeftRadius 才是右下角 -->
    <corners android:topLeftRadius="10dp" android:topRightRadius="10dp"
        android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/>
</shape>

三、介面佈局

整體佈局並不複雜,直接附上程式碼:

login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="https://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 為內邊距,layout_margin 為外邊距 -->
    <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" >
        <!-- 帳號 -->
        <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"/>
        <!-- 密碼 -->
        <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
            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>

四、Java 程式碼

Java 這邊相對單純,主要是實例化 Activity 並隱藏標題欄。

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);
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved