Sha*_*arj 5 android android-custom-view android-button android-actionbar searchview
I would like to add a custom button to SearchView widget.
Going through source code of search_view layout file I found out that it has a LinearLayout (submit_area) that contains submit button and voice button.
So I added a dynamic button to submit_area. But it doesn't show my dynamically added button because if either submit button or voice button is not enabled then submit_area is invisible. So it doesn't matter if I add anything dynamically.
SearchView searchView = (SearchView) menuItem.getActionView();
int
submit_areaId = searchView.getContext().getResources()
.getIdentifier("android:id/submit_area", null, null);
Button btnScan = new Button(getActivity());
btnScan.setText(getString(R.string.scan)); submitArea.addView(btnScan);
Run Code Online (Sandbox Code Playgroud)
However if I just set submit button enabled then it shows submit button as well as my dynamic button, like in the attached photo.
I just want to show my dynamic button only. Any clue how to achieve that?

searchView.setSubmitButtonEnabled(false);
LinearLayout linearLayoutOfSearchView = (LinearLayout) searchView.getChildAt(0);
Button yourButton = new Button(mContext); // and do whatever to your button
linearLayoutOfSearchView.addView(yourButton);
Run Code Online (Sandbox Code Playgroud)
SearchView由于正在设置私有类方法submit_area Visibility.Gone,因此我们对此无能为力。
一个快速的肮脏技巧是设置启用的默认按钮,然后将其宽度和高度设置为 0。设置Visibility不起作用,因此必须设置高度/宽度。
我启用了搜索按钮,然后将其高度/宽度设置为零。
int submit_areaId = searchView.getContext().getResources().getIdentifier("android:id/submit_area", null, null);
ImageView submitImage = (ImageView) searchView.findViewById(search_go_btnId);
searchView.setSubmitButtonEnabled(true);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, 0);
submitImage.setLayoutParams(layoutParams);
Run Code Online (Sandbox Code Playgroud)
现在SearchView将显示您在submit_area中添加的任何内容。
| 归档时间: |
|
| 查看次数: |
6704 次 |
| 最近记录: |