Android:如何使SearchView占据ActionBar中的整个水平空间? [英] Android: How to make the SearchView take up the entire horizontal space in ActionBar?
问题描述
在以下屏幕截图中,ActionBar
中的SearchView
小部件未占用全部可用空间.
In the following screenshot, the SearchView
widget in the ActionBar
does not take up the entire space available.
我需要它占用ActionBar
中存在的所有水平空间(因此它水平地横跨整个屏幕).
I need it to take up all the horizontal space present in the ActionBar
(so it spans the entire screen horizontally).
我尝试过的事情:
-
以编程方式,在
onCreateOptionsMenu
中尝试过此操作:
View searchViewView = (View) searchView;
searchViewView.getLayoutParams().width=LayoutParams.MATCH_PARENT;`
这给出了:
java.lang.NullPointerException: Attempt to write to
field 'int android.view.ViewGroup$LayoutParams.width' on a null
object reference ...
TestActionBarTwo.MainActivity.onCreateOptionsMenu(MainActivity.java:57)
,并在菜单资源中:
and in the menu resource:
android:layout_width="match_parent"
似乎没有影响
一切.
The android:layout_width="match_parent"
does not seem to affect
anything.
我也尝试过android:layout_weight="1"
(假设
ActionBar
具有LinearLayout
),但这也不起作用.
I also tried android:layout_weight="1"
(assuming that the
ActionBar
has a LinearLayout
), but that doesn't work either.
那么有没有办法使SearchView
(未图标化)占据ActionBar
的整个水平空间?
So is there a way that the SearchView
(not iconified) takes up the entire horizontal space of the ActionBar
?
编辑1
EDIT 1
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i(TAG, "onCreateOptionsMenu OF MainActivity CALLED."); //check
getMenuInflater().inflate(R.menu.activity_main_action_bar_menu, menu);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// SearchAction
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
//Adding event listeners
searchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.i(TAG, "onFocusChange OF ONFocusChangeListener IN MainActivity CALLED."); //check
}
});
return super.onCreateOptionsMenu(menu);
}
菜单资源为:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:testactionbartwo="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/mainActivity_searchActionTitle"
testactionbartwo:showAsAction="always"
testactionbartwo:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
即使我没有设置setIconifiedByDefault(false)
,即使它似乎无关紧要:`
Edit 2: Even when I DO NOT set setIconifiedByDefault(false)
, even though it does not seem relevant:`
代码更改:
// SearchAction
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
//searchView.setIconifiedByDefault(false);************************
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
结果,当我单击图标化的搜索图标以将其展开时:
Result, when I clicked the iconified search icon to expand it:
@MaheeraJazi的答案中复制的代码似乎仍然无效.我现在要发布整个SSCCE:
MainActivity.java:-
MainActivity.java:-
public class MainActivity extends ActionBarActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate OF MainActivity CALLED."); // check
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
Log.i(TAG, "onNewIntent OF MainActivity CALLED."); // check
handleIntent(intent);
setIntent(intent);
}
private void handleIntent(Intent intent) {
Log.i(TAG, "handleIntent OF MainActivity CALLED."); // check
if (intent.getAction().equals(Intent.ACTION_SEARCH)) {
showResults(intent.getStringExtra(SearchManager.QUERY));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i(TAG, "onCreateOptionsMenu OF MainActivity CALLED."); // check
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_action_bar_menu, menu);
// SearchAction
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchMenuItem = menu.findItem(R.id.mainActivity_actionSearch);
SearchView searchViewActionView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchViewActionView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchViewActionView.setIconifiedByDefault(true);
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
Log.i(TAG, "onQueryTextChange OF OnQueryTextListener IN MainActivity CALLED.");//check
// this is your adapter that will be filtered
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
Log.i(TAG, "onQueryTextChange OF OnQueryTextListener IN MainActivity CALLED.");//check
return true;
}
};
searchViewActionView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
Log.i(TAG, "onOptionsItemSelected OF MainActivity CALLED."); // check
switch (menuItem.getItemId()) {
case R.id.mainActivity_actionSearch:
onSearchRequested();
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
private void showResults(String searchQuery) {
Log.i(TAG, "showResults OF MainActivity CALLED."); // check
}
}
activity_main_action_bar_menu.xml:-
activity_main_action_bar_menu.xml:-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:testactionbartwo="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/mainActivity_actionSearch"
android:icon="@drawable/ic_action_search"
android:title="@string/mainActivity_searchActionTitle"
testactionbartwo:showAsAction="always"
testactionbartwo:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
activity_main.xml:-
activity_main.xml:-
<LinearLayout 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"
android:orientation="vertical" >
</LinearLayout>
清单文件:-
Manifest File:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="khanZarah.tests.TestActionBarTwo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light"
android:launchMode="singleTop"
android:uiOptions="splitActionBarWhenNarrow" >
<meta-data android:name="android.support.UI_OPTIONS"
android:value="splitActionBarWhenNarrow" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<meta-data android:name="android.app.default_searchable"
android:value=".MainActivity"/>
</application>
推荐答案
工具栏中搜索视图的默认宽度由非公共属性 abc_search_view_preferred_width 定义.所有屏幕尺寸均设置为320dp.
The default width of the search view in toolbar is defined by non-public attribute abc_search_view_preferred_width. It set to 320dp for all screen sizes.
一种解决方法是设置SearchView的maxWidth:
A workaround is to set maxWidth of SearchView:
searchView.setMaxWidth(Integer.MAX_VALUE);
这篇关于Android:如何使SearchView占据ActionBar中的整个水平空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!