简体   繁体   English

Android:动作栏中未显示图标

[英]Android : Icons not showing in actionbar

I am trying to show two icons in the actionbar, one of which is a menu with a few children. 我正在尝试在操作栏中显示两个图标,其中一个是带有几个孩子的菜单。 However, I am not able to. 但是,我不能。 I have tested a few answers from other similar questions in SO but to no avail. 我在SO中测试了其他类似问题的一些答案,但无济于事。 Not even one icon is showing up. 甚至没有一个图标出现。 Here is the code. 这是代码。 Thanks for your help. 谢谢你的帮助。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.semantic.semanticOrganizer.sem.activities.HomeActivity" >

<item android:id="@+id/viewTags"
    android:icon="@drawable/ic_action_labels"
    app:showAsAction="always"
    android:title="Tags"
    />

<item
    android:id="@+id/a_More"
    android:icon="@drawable/ic_more_vert_white_36dp"
    app:showAsAction="always"
    android:title="More">
    <menu>
<item android:id="@+id/action_view_notes"
    android:title="Notes"
    android:orderInCategory="100"
    app:showAsAction="never" />

<item android:id="@+id/action_view_habits"
    android:title="Habits"
    android:orderInCategory="100"
    app:showAsAction="never" />
<item android:id="@+id/action_view_checklists"
    android:title="Checklists"
    android:orderInCategory="100"
    app:showAsAction="never" />
<item android:id="@+id/action_view_tags"
    android:title="Tags"
    android:orderInCategory="100"
    app:showAsAction="never" />
<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    app:showAsAction="never" />
    </menu>

  </item>
</menu>

Java Code Java代码

public class HomeActivity extends Activity {

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

    stuffWhichDoesNotInvolveGettingActionBar();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.home, menu);
    return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    if (id == R.id.action_view_tags) {
        return true;

    }
    if (id == R.id.action_view_notes) {
        return true;
    }
    if (id == R.id.action_view_checklists) {
        return true;
    }
    if (id == R.id.action_view_habits) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


private void stuffWhichDoesNotInvolveGettingActionBar(){
   //Code
}

Edit: As Neutrino suggested, I tried to shift to appCompat Theme. 编辑:正如Neutrino所建议的,我试图转向appCompat主题。 But then this error showed up on Win64 machine. 但是,此错误出现在Win64计算机上。 Resource not found error to the attribute "actionModeShareDrawable". 属性“ actionModeShareDrawable”的资源未找到错误。 I changed the compileSdkVersion to 21 and it dint work. 我将compileSdkVersion更改为21并完成了工作。 So, I might have to do it without AppCompat for now. 因此,我现在可能不得不在没有AppCompat的情况下进行操作。 I am currently using Holo. 我目前正在使用Holo。 So please suggest a fix for Holo theme. 因此,请提出针对Holo主题的修复程序。

Edit I found a work around for the error I mentioned by using v20 of the support library instead of v21 since I found out that Gradle 2.3 build system is not around in Android yet and the bug was only fixed in that version (A Bug Fixed Jar of Groovy 2.3.5 was released). 编辑我找到了解决方法,通过使用支持库的v20而不是v21解决了我提到的错误,因为我发现Gradle 2.3生成系统尚未在Android中解决,并且该错误仅在该版本中得到了解决(一个错误已修复的Jar Groovy 2.3.5版已发布)。 Then I was able to use ActionBarActivity and now the action bar icons are visible. 然后,我可以使用ActionBarActivity,现在可以看到操作栏图标了。 Thanks everyone and especially Quark. 谢谢大家,尤其是夸克。

Extend ActionBarActivity instead of Activity , ie make 扩展ActionBarActivity而不是Activity ,即make

public class HomeActivity extends Activity 

as

public class HomeActivity extends ActionBarActivity

EDIT 编辑

ActionBarActivity is now deprecated to support toolbar. 现在不建议使用ActionBarActivity来支持工具栏。 To use toolbar, use - 要使用工具栏,请使用-

public class HomeActivity extends AppCompatActivity

Be sure to include the latest version of support library in build.gradle (Module: app) - 确保在build.gradle (Module: app)包含最新版本的支持库build.gradle (Module: app) -

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

Read more about toolbar and AppCompatActivity here , here and here . 在此处此处此处阅读有关工具栏和AppCompatActivity更多信息。

I tried compiling your code and the following worked for me. 我尝试编译您的代码,以下代码对我有用。 Replace app:showAsAction with android:showAsAction for all the menu items for which you want an icon to appear. 将要显示图标的所有菜单项替换为android:showAsAction app:showAsAction

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

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