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

Android 對話框圓角實作:用 shape 拆分標題欄與內容區

編輯:關於 Android 程式設計

剛接手公司的 Android 專案,設計稿丟來的需求是這樣:

彈出視窗四角要圓角,標題欄顏色與下方內容區不同,中間還要用藍色線分隔。

基本思路:shape + corners

搜一輪之後,主流方案都是在 drawable 資料夾底下放一個 shape 檔,用元素描述形狀。之所以要放 drawable,以及各元素的詳細語法,可以參考 Android 官方文件或資深前輩的講解(例如 https://blog.csdn.net/lonelyroamer/article/details/8254592 寫得很細)。

shape 的價值在於它提供了 solid、gradient、stroke、padding、corners 這組元素,其中 corners 能分別控制四個角的半徑。這正是圓角視窗需要的:

<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!-- 填滿色 -->
    <solid android:color="#0000FF" />

    <!-- 四角弧形;未指定的角預設半徑為 0 -->
    <corners
        android:topLeftRadius="@dimen/rounded_amplitude"
        android:topRightRadius="@dimen/rounded_amplitude" />

    <gradient
        android:angle="270"
        android:centerColor="#0000FF"
        android:endColor="#0000FF"
        android:startColor="#0000FF" />
</shape>

要特別注意的是:corners 只寫設定的那幾個角會生效,沒寫的角預設半徑為 0(也就是直角)。這一點上機跑一次就驗證得了。

定義好之後,在任何 Layout、View、TextView 上加上 android:background="@drawable/shapeyuanjiao3" 就能套用,也能只讓特定某個角圓潤。

拆解版面:標題與內容各自一個 shape

先看雛形結構:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.pop.main.clipimageview
        android:id="@+id/front_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#33000000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp"
        android:background="@drawable/shapeyuanjiao"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="30dip"
            android:background="@drawable/shapeyuanjiao3"
            android:gravity="center"
            android:text="保證金狀態"
            android:textColor="@color/white" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:background="@color/blue" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="30dip"
            android:text="帳號 09102"
            android:textColor="@color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="30dip"
            android:text="浮動損益"
            android:textColor="@color/black" />
    </LinearLayout>
</RelativeLayout>

關鍵在這一段:標題的 TextView 單獨套一個藍色 shape,整個視窗外框套另一個灰色 shape。兩個檔案的差異只在圓角涵蓋的角數與填色,複製一份改兩行即可。

外框那層長這樣:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginLeft="60dp"
    android:layout_marginRight="60dp"
    android:background="@drawable/shapeyuanjiao"
    android:orientation="vertical">

實際執行結果:

界面確實樸素,別嫌醜。過程繞了不少路,我把兩個坑列出來給同樣卡住的人參考。

彎路一:在外層再包一個圓角

網路上還有一種做法,用 shape 的 padding 搭配 stroke 撐出邊距來營造圓角:

<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <solid android:color="#0033FF" />

    <!-- 用邊距「假裝」圓角 -->
    <padding
        android:bottom="4dp"
        android:left="4dp"
        android:right="4dp"
        android:top="4dp" />

    <stroke
        android:width="1dp"
        android:color="#CdCdCd" />

    <corners android:radius="10dp" />
</shape>

這種寫法會讓整個視窗看起來像被額外包了一層,視覺邊界很彆扭,不符合這次的需求。有需要的可以參考,但別期待太好。

彎路二:子 View 的背景蓋掉外框圓角

一開始我的想法很直覺:標題要藍底,那就 android:background="@color/blue";外層 LinearLayout 套 shape 圓角。結果跑起來——一點圓角都沒有。

問了同事才恍然大悟:外層 Layout 雖然設了圓角 shape,但每一列子 View 一旦各自設了背景色,就會把外框的樣式整片蓋掉,圓角自然被埋在底下看不見。

解法很乾脆:子 View 不要設背景色,只讓標題那一列單獨套一個藍色的 shape,其餘保持透明。這樣外框的圓角才有機會露出來。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved