萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

Android 設置頁面圓角卡片佈局設計指南

設計目標

實現類似主流 App(QQ、微信)的設置頁面:分組卡片、圓角邊框、圖標+標題+右側箭頭,支援長列表滾動。

圓角實現方案

方案一:.9.png 九宮格圖片(推薦)

製作一張包含圓角的 .9.png 背景圖,指定可拉伸區域與內容區域,設置給每個設置項的容器(如 TableRow)。優點:性能好、適配性強、無過度繪製。

九宮格圓角背景示例

方案二:Shape Drawable(純代碼)

<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/white" />
    <corners android:radius="8dp" />
    <stroke android:width="1px" android:color="#ddd" />
</shape>

適用於簡單矩形圓角,複雜陰影/漸變較難實現。

整體佈局結構

<RelativeLayout android:background="#ffefebe7">
    <!-- 頂部標題欄 -->
    <RelativeLayout android:id="@+id/top_relative"
        android:background="#106184"
        android:layout_height="50dp">
        <TextView android:text="設置" ... />
    </RelativeLayout>

    <!-- 可滾動內容區 -->
    <ScrollView android:layout_below="@id/top_relative">
        <LinearLayout android:orientation="vertical" android:padding="10dp">

            <!-- 分組 1 -->
            <TableLayout>
                <TableRow android:background="@drawable/more_item_press">
                    <TextView android:drawableLeft="@drawable/mylike"
                        android:text="我的設置" />
                    <ImageView android:src="@drawable/ic_arrow" />
                </TableRow>
            </TableLayout>

            <TextView android:text="我的設置" android:textColor="#888888" /> <!-- 分組標題 -->

            <!-- 分組 2 -->
            <TableLayout>
                <TableRow android:background="@drawable/more_itemtop_press">
                    ...
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

關鍵點總結

  • 使用 ScrollView 包裹垂直 LinearLayout 應對項目較多情況
  • 每組設置用 TableLayout + TableRow 實現等寬佈局
  • 分組首項用 more_itemtop_press(頂部圓角),尾項用 more_itembottom_press(底部圓角),單項則用全圓角背景
  • TextView 的 drawableLeft 顯示圖標,drawablePadding 控制間距

現代替代方案

目前推薦使用 RecyclerView + CardView 實現,配合 MaterialCardView 可輕鬆實現圓角、陰影、Ripple 效果,代碼更簡潔且性能更優。

copyright © 萬盛學電腦網 all rights reserved