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

Android 實現 EditText 圓角的方法

編輯:關於 Android 程式設計

想讓輸入框(EditText)呈現圓角外觀,只要透過一個自訂的 shape 背景檔就能輕鬆搞定,完全不需要圖片素材。

步驟一:在 drawable 下新增 rounded_editview.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF"></solid>
    <padding android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp"></padding>
    <corners android:radius="15dp"></corners>
</shape>

其中 <corners android:radius> 決定圓角的大小,<solid> 設定背景填色,<padding> 則控制內容與邊框的間距。

步驟二:在 EditText 的 background 屬性中引用這個 XML

<EditText
    android:id="@+id/et_username"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:background="@drawable/rounded_editview"
    android:hint="@string/text_hint_username"/>

只要把 android:background 指向剛才定義的 shape 檔,輸入框就會呈現圓角效果。想調整圓角弧度時,改一下 radius 數值即可,非常直覺。

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