简体   繁体   English

Android ActionBar未显示图标

[英]Android ActionBar is not displaying icons

I'm having troubles with the ActionBar of my application, the problem is that is still showing the old android menu. 我的应用程序的ActionBar遇到麻烦,问题是仍然显示旧的Android菜单。 I have checked other awnsers to this question but nothing has worked. 我已经检查了其他问题,但没有任何效果。 Mi minimum app SDK allowed is 7 but I'm running in a device with Android 4.2. Mi最小应用SDK允许为7但我在Android 4.2的设备上运行。

I imported 我进口了

import android.support.v7.app.ActionBarActivity;

This is my onCreate() method 这是我的onCreate()方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    inputTextView = (EditText) findViewById(R.id.inputText);
    outputTextView = (TextView) findViewById(R.id.outputText);
    inputTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
            showMessage((View) textView);
            return true;
        }
    });
    registerForContextMenu(inputTextView);
}

This is my onCreateOptionsMenu 这是我的onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.mainmenu, menu);
    return true;
}

This is my XML 这是我的XML

<item
    android:id="@+id/menu"
    android:orderInCategory="0"
    android:showAsAction="always"
    android:icon="@drawable/abc_ic_cab_done_holo_dark"
    >

</item>
<item
    android:id="@+id/lol"
    android:orderInCategory="0"
    android:showAsAction="ifRoom"
    android:icon="@drawable/abc_ic_go">
</item>

Here is my appManifest.xml, I know that it miss the package (I removed it and I'm trying to add it) 这是我的appManifest.xml,我知道它错过了包(我删除了它,我正在尝试添加它)

<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19"/>


<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
    <activity android:label="@string/app_name" android:name="com.ricardo.message.MainActivity" android:theme="@style/Theme.AppCompat.Light">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

Thanks for the help! 谢谢您的帮助!

Per the Adding Action Items guide , you must use the http://schemas.android.com/apk/res-auto schema version of showAsAction , rather than android:showAsAction : 根据添加操作项指南 ,您必须使用showAsActionhttp://schemas.android.com/apk/res-auto架构版本,而不是android:showAsAction

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu"
        android:orderInCategory="0"
        app:showAsAction="always"
        android:icon="@drawable/abc_ic_cab_done_holo_dark" />
    <item
        android:id="@+id/lol"
        android:orderInCategory="0"
        app:showAsAction="ifRoom"
        android:icon="@drawable/abc_ic_go" />
</menu>
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);

OR 要么

Make a XML layout call the tool_bar.xml 使XML布局调用tool_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:elevation="4dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:src="@drawable/ic_action_search"/>

    </RelativeLayout>
</android.support.v7.widget.Toolbar>

Now in you main activity add this line 现在在你的主要活动中添加这一行

 <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar" />

Use this xmlns:android="http://schemas.android.com/apk/res/android" 使用此xmlns:android =“http://schemas.android.com/apk/res/android”

Try this code 试试这个代码

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- Adds an item to the list. -->
<item
    android:id="@+id/action_add"
    android:icon="@drawable/ic_action_new"
    android:showAsAction="ifRoom"
    android:title="@string/action_add"/>
<item
    android:id="@+id/action_search"
    android:actionViewClass="android.widget.SearchView"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="collapseActionView|ifRoom"
    android:title="@string/action_search"/>

</menu>

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

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