3

我想在我的应用程序中放置实现搜索,因为我需要制作一个搜索栏,就像我们在文本字段末尾的谷歌栏图像中看到的那样。我尝试使用 FrameLayout 并放置 EditText 和图像,但它不起作用..我该如何制作。

4

4 回答 4

2

只需使用 RelativeLayout 在 EditText 顶部放置一个小的 ImageView。不要忘记调整 EditText 的填充,使文本不会出现在 ImageView 下。

于 2013-02-05T18:34:20.837 回答
1

在您的 EditText 中使用它

android:drawableRight="@drawable/search"

这会将图像放在您的 EditText 中..我希望这会对您有所帮助..

于 2013-02-05T18:36:53.640 回答
1

以编程方式您可以做到这一点。

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

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

或者也

android:drawableRight="@drawable/search"

正如普拉尼亚尼所建议的那样。

于 2013-02-05T18:44:19.787 回答
1

我认为解决这个问题的最好方法是使用行动项目。在 Android 操作栏上查看此链接。

将搜索视图添加到操作栏中非常简单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_search"
          android:title="@string/menu_search"
          android:icon="@drawable/ic_menu_search"
          android:showAsAction="ifRoom|collapseActionView"
          android:actionViewClass="android.widget.SearchView" />
</menu>

正如上面提到的链接中所解释的,这里是如何访问它:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.options, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    // Configure the search info and add any event listeners
    ...
    return super.onCreateOptionsMenu(menu);
}
于 2013-02-05T18:38:33.647 回答