如何在 EditText 上新增圖片?


本示例演示瞭如何在 EditText 上新增影像。

步驟 1 − 在 Android Studio 中建立一個新專案,轉到檔案 ⇒ 新專案,並填寫所有必需的詳細資訊以建立新專案。

步驟 2 − 將以下程式碼新增到 res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:gravity="center"
   android:layout_marginTop="30dp"
   tools:context=".MainActivity">
   <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
      android:paddingStart="5dp"
      android:background="@drawable/rounded_edittext"
      android:drawableRight="@android:drawable/ic_menu_search"
      android:paddingLeft="5dp" />
</LinearLayout>

在上面的程式碼中,我們取了編輯文字並添加了背景作為 background.xml。

步驟 3 − 將以下程式碼新增到 drawable/background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
   <solid android:color="#FFFFFF" />
   <stroke
      android:width="1dp"
      android:color="#2f6699" />
   <corners
      android:radius="10dp" />
</shape>

讓我們嘗試執行你的應用程式。我假設你已將 Android 移動裝置與計算機連線。從 Android Studio 執行應用程式,開啟其中一個專案的活動檔案,然後單擊工具欄中的執行 圖示。選擇你的移動裝置作為選項,然後檢查你的移動裝置,它將顯示你的預設螢幕——

單擊此處下載專案程式碼

更新日期: 2020 年 6 月 27 日

491 次瀏覽

開啟您的職業生涯

完成課程,獲得認證

開始學習
廣告