我遇到了一个非常奇怪的问题。当我点击SearchView时,我的LinearLayout会立即向下移动一些像素,大约10或20.为了说明这种情况,我会显示一些截图。
在点击SearchView之前:
点击SearchView后:
这是OnCreateOptionsMenu方法:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d(LOG_TAG,"OnCreateOptionsMenu-FragmentRegion");
inflater.inflate(R.menu.menu_search,menu);
final MenuItem menuItem=menu.findItem(R.id.action_search);
SearchManager manager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView= (SearchView) MenuItemCompat.getActionView(menuItem);
//hint color
SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setHint("Stock/Index");
if(Build.VERSION.SDK_INT>=16) {
View searchplate = (View) searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
final Drawable searchLine = ContextCompat.getDrawable(getActivity(), R.drawable.abc_textfield_search_material);
searchLine.setColorFilter(ContextCompat.getColor(getActivity(), R.color.colorAccent), PorterDuff.Mode.SRC_ATOP);
searchplate.setBackground(searchLine); // this method requires api level 16
}
searchView.setSearchableInfo(manager.getSearchableInfo(getActivity().getComponentName()));
mSuggestionsAdapter =new SuggestionsAdapter(getActivity(),null,SUGGESTIONS_LOADER);
searchView.setSuggestionsAdapter(mSuggestionsAdapter);
//on first launch will be always null, after rotating not
if(mSearchFilter!=null) {
MenuItemCompat.expandActionView(menuItem);
searchView.setQuery(mSearchFilter, true);
}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
ViewGroup.MarginLayoutParams params =
(ViewGroup.MarginLayoutParams)lLRegion.getLayoutParams();
//LinearLayout.LayoutParams params= (LinearLayout.LayoutParams)
params.topMargin=-10;
lLRegion.setLayoutParams(params);
lLRegion.requestLayout();
Log.d(LOG_TAG,"OnQueryTextChange");
mSearchFilter= TextUtils.isEmpty(newText) ? null: newText;
getLoaderManager().restartLoader(SUGGESTIONS_LOADER,null,FragmentRegion.this);
return true;
}
});
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int position) {
// Cursor c= (Cursor) mSuggestionsAdapter.getItem(position);
// c.close();
return false;
}
@Override
public boolean onSuggestionClick(int position) {
Cursor c = (Cursor) mSuggestionsAdapter.getItem(position);
String symbol = c.getString(SuggestionsAdapter.COL_TICKER);
String companyName = c.getString(SuggestionsAdapter.COL_NAME);
String securityType = c.getString(SuggestionsAdapter.COL_SECURITY_TYPE);
((Callback)getActivity()).onItemSelected(symbol,companyName,securityType.toUpperCase());
//clear focus for when returning from the previous activity
searchView.clearFocus();
MenuItemCompat.collapseActionView(menuItem);
return true;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
正如你所看到的,我试图以编程方式在onQueryTextChange方法中插入一个上边距,但没有成功。
这是xml
ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/llRegion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.linearlistview.LinearListView
android:id="@+id/indexesList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<com.linearlistview.LinearListView
android:id="@+id/newsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</LinearLayout>
</ScrollView>
提前致谢!
答案 0 :(得分:0)
将此行添加到清单中解决了我的问题:
android:windowSoftInputMode="adjustPan"
在活动代码
中<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustPan"
android:launchMode="singleTop"
android:theme="@style/AppTheme1">