How to fix cut-off custom SearchView background in landscape?

不打扰是莪最后的温柔 提交于 2019-12-05 12:37:26

You have to use a smaller version of the edit text. The drawables instrinsic dimensions are smaller than the default edit text drawables.

Check out the drawables used here

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r2.1/frameworks/support/v7/appcompat/res/drawable/abc_textfield_searchview_holo_dark.xml?av=f

I had a similar problem, and was able to fix it by simply changing the Gravity of the EditText:

try {
    int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
    AutoCompleteTextView searchPlate = (AutoCompleteTextView) searchView.findViewById(searchPlateId);

    //fixes the gravity (fixes text cutoff for landscape mode)
    searchPlate.setGravity(Gravity.LEFT|Gravity.BOTTOM);
}
catch (Throwable t)
{
    //not sure if the above code will always work (layouts change), but works well for the current API.
    t.printStackTrace();
}
lucent75

The searchView is not easily customized but I did find a work around. My research and code is mostly taken from the first answer found here: Changing the background drawable of the searchview widget. After setting the background in my XML file like this:

       `<SearchView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="@drawable/rounded_grey_search_bar"
        android:clickable="true"
        android:iconifiedByDefault="false"/>`

I added the following code to my Java file to alter the padding of the SearchView:

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);


    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
    searchView.findViewById(searchPlateId).setBackgroundResource(R.drawable.rounded_grey_search_bar);

    int voiceSearchPlateId = searchView.getContext().getResources().getIdentifier("android:id/submit_area", null, null);
    searchView.findViewById(voiceSearchPlateId).setBackgroundResource(R.drawable.rounded_grey_search_bar);

    int searchTextViewId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
    TextView searchTextView = (TextView) searchView.findViewById(searchTextViewId);
    searchTextView.setPadding(20, 20, 20, 20);

Setting the padding allowed me to get the text where I wanted it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!