如何在編輯框中設定 ImageView?


此示例演示瞭如何在編輯文字中設定影像檢視。

步驟 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:drawableStart="@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-06-27

209 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始吧
廣告
© . All rights reserved.