2010-11-26 69 views

回答

342

在EditText上使用android:drawableLeft属性。

<EditText 
    ...  
    android:drawableLeft="@drawable/my_icon" /> 
+0

请给我更多详情 – Adham 2010-11-26 00:36:22

+3

对不清楚的问题抱歉,我想添加一个按钮,所以当我点击它时,它会执行一些操作 – Adham 2010-11-26 00:38:32

+19

考虑到搜索标题,这只是我期望的解决方案。 – 2010-12-18 22:35:50

0

使用相对布局,设置按钮被左对齐编辑文本视图的,和你的文本视图的左填充设为您的按钮的大小。我想不出一个好办法,没有硬编码填充:/

您还可以使用apk tool排序解压facebook apk并查看其布局文件。

25

您可以按marcos建议的那样在XML中设置drawableLeft,但您可能还希望以编程方式对其进行设置 - 例如响应事件。要做到这一点使用方法setCompoundDrawablesWithIntrincisBounds(int, int, int, int)

EditText editText = findViewById(R.id.myEditText); 

// Set drawables for left, top, right, and bottom - send 0 for nothing 
editTxt.setCompoundDrawablesWithIntrinsicBounds(R.drawable.myDrawable, 0, 0, 0); 
18

如果您想使用Android的默认绘制,你可以使用@android:drawable/ic_menu_search这样的:

<EditText android:id="@+id/inputSearch" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:drawableLeft="@android:drawable/ic_menu_search" 
    android:hint="Search product.." 
    android:inputType="textVisiblePassword"/> 
0

用途:

<EditText 
    ..  
    android:drawableStart="@drawable/icon" /> 
相关问题