Ars*_*Ali 38 android imagebutton android-layout android-edittext
我想在EditText中放置一个图像按钮,但我没有想法,请告诉我如何操作,如图所示.谢谢

Nir*_*tel 50
如果您不想单击该图像,那么您可以使用drawableRightEditText属性..
android:drawableRight="@drawable/icon"
Run Code Online (Sandbox Code Playgroud)
如果您想要点击,请使用下面的代码.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter search key" />
<ImageButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/search"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:text="Button"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
Moh*_*ikh 33
如果你想要这样的布局和图像可点击,那么你可以做这样的事情
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true" >
</EditText>
<ImageView
android:id="@+id/imageView1"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignRight="@+id/editText1"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
输出:

Mey*_*ann 16
在Material中,EditText下有下划线.在大多数应用程序中,图标显示在下划线内.
为此,只需使用填充
android:paddingEnd = "@dimen/my_button_size"
Run Code Online (Sandbox Code Playgroud)
不要忘记sub 4.2版本的paddingRight.
<RelativeLayout ....>
...
<EditText
...
android:id="@+id/my_text_edit"
android:textAlignment = "viewStart"
android:paddingEnd = "@dimen/my_button_size"
android:paddingRight = "@dimen/my_button_size"
.../>
<ImageButton
....
android:layout_width="@dimen/my_button_size"
android:layout_height="@dimen/my_button_size"
android:layout_alignEnd="@id/my_text_edit"
android:layout_alignRight="@id/my_text_edit"/>
...
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
它会将文本限制为图像,并且图像不会与文本重叠,无论文本多长.
我的解决方案适合所有屏幕尺寸而不会editText"落后" imageButton.这也使用了android资源,因此您不需要提供自己的图标或字符串资源.将此代码段复制并粘贴到您想要搜索栏的任何位置:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_margin="8dp"
android:id="@+id/etSearch"
android:hint="@android:string/search_go"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/ivSearch"
android:background="@color/transparent_black"
android:padding="2dp"
android:layout_width="wrap_content"
android:layout_margin="8dp"
android:src="@android:drawable/ic_menu_search"
android:layout_height="wrap_content"
tools:ignore="contentDescription" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Restult:

| 归档时间: |
|
| 查看次数: |
55359 次 |
| 最近记录: |