Android SearchView 自定义
Android SearchView customisation
我是 android 的新手,我被困在一些我认为很简单但我很困惑的事情上..
我需要在我的 Relativelayout 中而不是 actionbar/toolbar 中创建自定义 searchView。问题是我不知道如何自定义背景、文本输入颜色、XML 中的搜索图标颜色,或者只是它们的属性。目前我在 phone 上,无法显示我的代码。有人告诉我如何自定义它吗?也许向我展示任何我可以遵循的教程?提前致谢!!!!
将此代码添加到您的 RelativeLayout
-
<android.support.v7.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAsh"
android:backgroundTint="@color/colorAsh"
android:searchIcon="@drawable/ic_search"
android:commitIcon="@drawable/ic_commit"
android:searchHintIcon="@drawable/ic_search"
android:closeIcon="@drawable/ic_close"
android:goIcon="@drawable/ic_go">
</android.support.v7.widget.SearchView>
别忘了更改这些图标。
并按照此 SearchView 了解 SearchView
的每个选项。
自己做搜索视图就行了,很简单。
custom_searchview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:gravity="center_vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_search_white"/>
<EditText
android:id="@+id/edt_search_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text"
android:imeOptions="actionSearch"
android:hint="@string/by_name"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:lines="1"
android:textColorHint="@color/colorTextGrey"
android:textColor="@color/white"
android:textSize="14sp"
android:background="@android:color/transparent"/>
<ImageView
android:id="@+id/iv_clear_text"
android:layout_width="25dp"
android:layout_height="25dp"
android:adjustViewBounds="true"
android:visibility="gone"
android:src="@drawable/ic_clear"/>
</LinearLayout>
然后您可以将此布局包含在您的 activity 布局文件中。这是一个简单的布局,其中包括一个 "search icon",然后是 EditText,然后是 "clear icon"。用户键入一些文本后会显示清除图标。
然后在您的 activity 中,您需要在用户单击键盘上的搜索时收听,并在用户输入要显示的文本时收听 "clear icon"
/*search btn clicked*/
edtSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
}
});
/*hide/show clear button in search view*/
edtSearchText.addTextChangedListener(searchViewTextWatcher);
TextWatcher searchViewTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.toString().trim().length()==0){
ivClearText.setVisibility(View.GONE);
} else {
ivClearText.setVisibility(View.VISIBLE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
我建议你看看我在这里给出的答案来设计你自己的 SearchBar。
我不明白原因,但我无法设置 Google 的默认搜索栏样式,所以我不得不创建自己的搜索栏。
这是我的做法:
如果你想实现即时搜索算法,请关注这位伟大的作者和他的项目(查看已接受的答案):
如果您需要进一步的帮助,请发表评论。
如果答案对我的答案有帮助,请点赞 :P
更新:这是 Google 搜索视图的实现,只是为了给您提供更多信息:https://developer.android.com/guide/topics/search/search-dialog.html
祝你有愉快的一天:)
frament_active.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragment.ActiveFragment">
<LinearLayout
android:layout_margin="30dp"
android:background="@drawable/searchview_background"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:gravity="center_vertical">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:adjustViewBounds="true"
android:visibility="gone"
android:src="@drawable/ic_search"/>
<EditText
android:padding="16dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text"
android:imeOptions="actionSearch"
android:hint="Search Items"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:lines="1"
android:textColorHint="#653201"
android:textColor="#653201"
android:textSize="14sp"
android:background="@android:color/transparent"/>
<ImageView
android:layout_marginRight="16dp"
android:layout_width="20dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_search"/>
</LinearLayout>
searchview_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp" />
<solid android:color="#EFE0BB"/>
<stroke android:color="#EFE0BB" android:width="1dp"/>
</shape>
我是 android 的新手,我被困在一些我认为很简单但我很困惑的事情上.. 我需要在我的 Relativelayout 中而不是 actionbar/toolbar 中创建自定义 searchView。问题是我不知道如何自定义背景、文本输入颜色、XML 中的搜索图标颜色,或者只是它们的属性。目前我在 phone 上,无法显示我的代码。有人告诉我如何自定义它吗?也许向我展示任何我可以遵循的教程?提前致谢!!!!
将此代码添加到您的 RelativeLayout
-
<android.support.v7.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAsh"
android:backgroundTint="@color/colorAsh"
android:searchIcon="@drawable/ic_search"
android:commitIcon="@drawable/ic_commit"
android:searchHintIcon="@drawable/ic_search"
android:closeIcon="@drawable/ic_close"
android:goIcon="@drawable/ic_go">
</android.support.v7.widget.SearchView>
别忘了更改这些图标。
并按照此 SearchView 了解 SearchView
的每个选项。
自己做搜索视图就行了,很简单。
custom_searchview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:gravity="center_vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_search_white"/>
<EditText
android:id="@+id/edt_search_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text"
android:imeOptions="actionSearch"
android:hint="@string/by_name"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:lines="1"
android:textColorHint="@color/colorTextGrey"
android:textColor="@color/white"
android:textSize="14sp"
android:background="@android:color/transparent"/>
<ImageView
android:id="@+id/iv_clear_text"
android:layout_width="25dp"
android:layout_height="25dp"
android:adjustViewBounds="true"
android:visibility="gone"
android:src="@drawable/ic_clear"/>
</LinearLayout>
然后您可以将此布局包含在您的 activity 布局文件中。这是一个简单的布局,其中包括一个 "search icon",然后是 EditText,然后是 "clear icon"。用户键入一些文本后会显示清除图标。
然后在您的 activity 中,您需要在用户单击键盘上的搜索时收听,并在用户输入要显示的文本时收听 "clear icon"
/*search btn clicked*/
edtSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
}
});
/*hide/show clear button in search view*/
edtSearchText.addTextChangedListener(searchViewTextWatcher);
TextWatcher searchViewTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.toString().trim().length()==0){
ivClearText.setVisibility(View.GONE);
} else {
ivClearText.setVisibility(View.VISIBLE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
我建议你看看我在这里给出的答案来设计你自己的 SearchBar。
我不明白原因,但我无法设置 Google 的默认搜索栏样式,所以我不得不创建自己的搜索栏。
这是我的做法:
如果你想实现即时搜索算法,请关注这位伟大的作者和他的项目(查看已接受的答案):
如果您需要进一步的帮助,请发表评论。 如果答案对我的答案有帮助,请点赞 :P
更新:这是 Google 搜索视图的实现,只是为了给您提供更多信息:https://developer.android.com/guide/topics/search/search-dialog.html
祝你有愉快的一天:)
frament_active.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragment.ActiveFragment">
<LinearLayout
android:layout_margin="30dp"
android:background="@drawable/searchview_background"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:gravity="center_vertical">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:adjustViewBounds="true"
android:visibility="gone"
android:src="@drawable/ic_search"/>
<EditText
android:padding="16dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text"
android:imeOptions="actionSearch"
android:hint="Search Items"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:lines="1"
android:textColorHint="#653201"
android:textColor="#653201"
android:textSize="14sp"
android:background="@android:color/transparent"/>
<ImageView
android:layout_marginRight="16dp"
android:layout_width="20dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_search"/>
</LinearLayout>
searchview_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp" />
<solid android:color="#EFE0BB"/>
<stroke android:color="#EFE0BB" android:width="1dp"/>
</shape>