首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android 圓角 EditText 背景:Shape drawable 定義上下不同圓角半徑

     編輯:程式設計綜合問答

實現頂部/底部圓角、中間直角的輸入框組:定義兩個 Shape drawable 分別用於頂部和底部 EditText,中間用無圓角 Shape。

頂部 EditText 背景

<!-- res/drawable/top_edittext_bg.xml -->
<shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#e2e2e2" />
    <corners 
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp"
        android:bottomLeftRadius="0.1dp"
        android:bottomRightRadius="0.1dp" />
</shape>

底部 EditText 背景

<!-- res/drawable/bottom_edittext_bg.xml -->
<shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#e2e2e2" />
    <corners 
        android:topLeftRadius="0.1dp"
        android:topRightRadius="0.1dp"
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp" />
</shape>

中間 EditText 背景(可選)

<shape ...>
    <solid android:color="#e2e2e2" />
    <corners android:radius="0.1dp" /> <!-- 基本無圓角 -->
</shape>

佈局使用

<LinearLayout android:orientation="vertical" ...>
    <EditText android:background="@drawable/top_edittext_bg" ... />
    <EditText android:background="@drawable/middle_edittext_bg" ... />
    <EditText android:background="@drawable/bottom_edittext_bg" ... />
</LinearLayout>

關鍵技巧

  • 0.1dp 而非 0dp 避免某些系統渲染異常。
  • 同色 solid 確保視覺無縫連接。
  • 若為單個 EditText 內部多行模擬,需自定義 View 繪製分段圓角。

Shape drawable 組合實現復雜圓角邊框,無需 9-patch 圖片。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved