在 EditText 或 TextView 最右側顯示一個圖標(如搜尋、清除、眼睛)。
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="40dp" /> <!-- 留出圖標空間 -->
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical|end"
android:layout_marginEnd="8dp"
android:src="@drawable/ic_search" />
</FrameLayout>
ImageView 換為 ImageButton 或設定 android:clickable="true" + setOnClickListener。TextView.setCompoundDrawablesRelativeWithIntrinsicBounds 動態設定右側圖標(API 17+)。
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:endIconMode="clear_text"> <!-- 或 password_toggle, custom -->
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>