Sol*_*ace 2 android android-layout layoutparams android-actionbar searchview
在下面的屏幕截图中,SearchView
窗口小部件ActionBar
不占用整个可用空间.
我需要它占据所有水平空间ActionBar
(因此它横跨整个屏幕).
我尝试了什么:
以编程方式,尝试了这个onCreateOptionsMenu
:
View searchViewView = (View) searchView;
searchViewView.getLayoutParams().width=LayoutParams.MATCH_PARENT;`
Run Code Online (Sandbox Code Playgroud)
这给出了:
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)
Run Code Online (Sandbox Code Playgroud)并在菜单资源中:
本android:layout_width="match_parent"
似乎并没有造成任何影响.
我也试过android:layout_weight="1"
(假设
ActionBar
有a LinearLayout
),但这也不起作用.
那么有没有一种方法SearchView
(没有图标化)占据整个水平空间ActionBar
?
编辑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);
}
Run Code Online (Sandbox Code Playgroud)
菜单资源是:
<?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>
Run Code Online (Sandbox Code Playgroud)
编辑2:即使我没有设置setIconifiedByDefault(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);************************
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
Run Code Online (Sandbox Code Playgroud)
结果,当我点击图标化搜索图标以展开它时:
编辑3:@MaheeraJazi从答案中复制的代码,似乎仍然不起作用.我现在发布整个SSCCE:
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
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
清单文件: -
<?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>
Run Code Online (Sandbox Code Playgroud)
工具栏中搜索视图的默认宽度由非公共属性abc_search_view_preferred_width定义.所有屏幕尺寸均设置为320dp.
解决方法是设置SearchView的maxWidth:
searchView.setMaxWidth(Integer.MAX_VALUE);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5198 次 |
最近记录: |