简体   繁体   中英

Icon search doesn't appear in toolbar - Android 4.4

guys I develop an app in Java for Android (min SDK 19), and I try to add a search button in my app but this icon don't appear. Please see the code below:

This is my search.xml in @menu folder

<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http;//schemas.android.com/apk/res-auto">
<item
    android:id="@+id/search"
    android:icon="@drawable/ic_search"
    android:title="@string/search"
    android:orderInCategory="100"
    app:showAsAction="ifRoom"/>
</menu>

This is may Activity Main WHY my icon search dosen't appear? because I already added the icon in @drawble

package com.squarcy.equals;

import android.support.v7.widget.Toolbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.res.Resources;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Toolbar
    Toolbar toolbar = findViewById(R.id.toolbar);
    TextView title = toolbar.findViewById(R.id.toolbar_title);
    setSupportActionBar(toolbar);
    title.setText(toolbar.getTitle());
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
}

// Get Status Bar Height
private int getStatusBarHeight() {
    int height;
    Resources myResources = getResources();
    int idStatusBarHeight = myResources.getIdentifier(
            "status_bar_height", "dimen", "android");
    if (idStatusBarHeight > 0) {
        height = getResources().getDimensionPixelSize(idStatusBarHeight);
    }else{
        height = 0;
    }
    return height;
    }

    // Inflate Search
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search,menu);
    return true;
    }

    // If Icon Search is Clicked
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }
    }

Android模拟器

字串搜寻

Try first setting showAsAction="always" and removing this line toolbar.setPadding(0, getStatusBarHeight(), 0, 0); to be sure the padding is not interfering in the render process of the search button.

I finally found the solution!! Android Studio was giving me the error "should use app:showAsAction with the appcompat library with xmlns:app="schemas.android.com/apk/res-auto". If I changed my XML as suggested, my menus in the actionBar disappeared into the overflow.

Which imported the appcompat library, and caused all the trouble. Since I only targeted Android 4.4 and up, I was able to remove these two lines. Problem solved!

The real culprits turned out to be the following lines in the file build.gradle:

dependencies {
    …
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:support-v4:22.1.1'
}

And this for me search.xml

 <item
        android:id="@+id/search"
        android:title="@string/search"
        android:icon="@drawable/ic_search"
        android:orderInCategory="100"
        app:showAsAction="ifRoom" />

解决了

More Info: https://developer.android.com/about/versions/android-4.4

Also can help: Item with app:showAsAction not showing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM