SearchView on support.v7.appcompat library issue:

2020-04-02 07:45发布

问题:

I'm developing an app with ActionBar using support.v7.appcompat library. The action bar works, the SearchView is shown, the hint shows. The only problem is that the background of the SearchView is not scalling properly. Insead of the usual, it appears big and with the 9-patch black lines.

Using:

  • Developing from command line, using ant debug to compile.
  • On Linux Mageia 3, ant version: Apache Ant(TM) version 1.8.4 compiled on January 11 2013
  • Linking to library with project.properties line:

android.library.reference.1=../../../../../sdk/extras/android/support/v7/appcompat/

  • Using @style/Theme.AppCompat.Light
  • Tested in device with CM10-1, in device with stock 4.1, and emulator with API 8 (Android 2.2). Same result in all devices.

Screenshot:

Code:

DiccionariCatala.java (main activity):

import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.support.v4.view.MenuItemCompat;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;


public class DiccionariCatala extends ActionBarActivity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }   

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_actions, menu);
        SearchManager SManager =  (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        SearchView searchViewAction = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
        searchViewAction.setSearchableInfo(SManager.getSearchableInfo(getComponentName()));
        searchViewAction.setIconifiedByDefault(false);
        return super.onCreateOptionsMenu(menu);
    }   

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()) {
                case R.id.action_search:
                    //openSearch();
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
        }   
    }      
}

menu_actions.xml (menu xml)

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:diccionaricatala="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
          android:title="@string/action_search_title"
          diccionaricatala:showAsAction="ifRoom"
          diccionaricatala:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mypackage.apps"
      android:versionCode="1"
      android:versionName="1.0">
      <uses-sdk android:minSdkVersion="8"  android:targetSdkVersion="16"/>
      <application android:label="@string/app_name"
                   android:icon="@drawable/ic_launcher"
                   android:theme="@style/Theme.AppCompat.Light">

       <activity android:name="DiccionariCatala"
                 android:label="@string/app_name">
            <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>
   </application>
</manifest>

project.properties

target=android-16
android.library.reference.1=../../../../../sdk/extras/android/support/v7/appcompat/

Any help would me much apreciated. I can't find anything wrong, and there's also the weird fact that despide this, everything else work. Thanks.

回答1:

Ok, I want to kill myself. I've fixed it. Don't know how. I can't reproduce the bug again!

what I think has fixed the bug: I've added import android.support.v7.app.ActionBar;

Then I've removed it to reproduce the bug, but it's not back.

I also did uninstall Build-tools 18.0.1 (which is in italics and made me suspicious), so I thought it might be the source of the bug. But I've reinstalled it, and can't reproduce the bug either.

I don't know what else could possibly be. Anyway, moving on... (sight)

Edit:

It happened again, and this time I've been able to narrow it down. Turns out it is a bug of Build Tools v18.*, downgrading to v17 solves the issue.



回答2:

This is usually an issue where the resources (R) file has not been correctly generated and some references are misaligned.

Doing a clean is usually a good fix, as it regenerates this file. This is what you inadvertently did when reinstalling.



回答3:

I had the same issue with build tools 19.0.1, but newer version 19.0.3 fixed the problem. Try it, hopefully it will help you too.