Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

在 Android 中打造 iOS 風格的自訂按鈕

iOS 風格的 UI 一直頗受歡迎,許多 Android 設計者也希望能把介面做得貼近 iOS 的質感。下面就用一個實例,示範如何透過 Shape 定義出一顆 iOS 風格的按鈕。

首先定義 ios_button_grouped.xml:

<?xml version="1.0" encoding="UTF-8"?>
<selector
    xmlns:android="https://schemas.android.com/apk/res/android">
    <item
       android:state_pressed="true"
       android:drawable="@drawable/ios_button_grouped_pressed" />
    <item
       android:state_focused="true"
       android:state_window_focused="true"
       android:drawable="@drawable/ios_button_grouped_pressed" />
    <item
       android:drawable="@drawable/ios_button_grouped_normal" />
</selector>

接著定義 ios_button_grouped_normal.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#eee" />
    <stroke android:width="1" android:color="@android:color/black" />
    <corners android:radius="5" />
</shape>

再來定義 ios_button_grouped_pressed.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#0be" />
    <stroke android:width="1" android:color="@android:color/black" />
    <corners android:radius="5" />
</shape>

這裡的核心,是定義 Shape 中矩形(rectangle)的填色。除此之外還有許多可設定的顏色屬性,例如 startColor、endColor、angle 等等,善用它們便能勾勒出更精美的 UI 效果,建議搭配 API 文件一併參考。

使用方法很簡單:把它當作背景圖片套用即可:

android:background="@drawable/ios_grouped_normal"

Copyright © Linux教程網 All Rights Reserved