简体   繁体   English

图标搜索未出现在工具栏中-Android 4.4

[英]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. 伙计们,我用Java开发了适用于Android的应用程序(最低SDK 19),然后尝试在应用程序中添加搜索按钮,但此图标没有出现。 Please see the code below: 请参见下面的代码:

This is my search.xml in @menu folder 这是我的@menu文件夹中的search.xml

<?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? 这可能是Activity Main为什么我的图标搜索没有出现? because I already added the icon in @drawble 因为我已经在@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); 尝试首先设置showAsAction="always"并删除此工具showAsAction="always" 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. Android Studio给我一个错误“应将app:showAsAction与xmlns:app =“ schemas.android.com/apk/res-auto的appcompat库一起使用”。如果我按建议更改了XML,则actionBar中的菜单消失了进入溢出。

Which imported the appcompat library, and caused all the trouble. 哪个导入了appcompat库,并造成了所有麻烦。 Since I only targeted Android 4.4 and up, I was able to remove these two lines. 由于我仅针对Android 4.4及更高版本,因此我可以删除这两行。 Problem solved! 问题解决了!

The real culprits turned out to be the following lines in the file build.gradle: 真正的罪魁祸首是文件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 而这对我来说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 更多信息: https : //developer.android.com/about/versions/android-4.4

Also can help: Item with app:showAsAction not showing 也可以帮助: 带有app:showAsAction的项目不显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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