简体   繁体   English

Android应用程序图标未显示在操作栏上

[英]Android app icon doesn't show on actionbar

I am creating an android app using eclipse and I have a problem. 我正在使用eclipse创建一个android应用,但是我遇到了问题。 the app icon doesn't show on actionbar. 该应用程序图标未显示在操作栏上。 can anybody tell me what's wrong? 谁能告诉我怎么了? here is the manifest: 这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gowemto.gnoulashe"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".CalcResultActivity"
            android:label="@string/title_activity_calc_result"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.gowemto.gnoulashe.MainActivity" />
        </activity>

    </application>

</manifest>

and here's the main activity code: 这是主要的活动代码:

package com.gowemto.gnoulashe;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends ActionBarActivity {
    String result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    } 



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @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;
        }
        return super.onOptionsItemSelected(item);
    }



   public void intent1(View view) {  
         Intent intent = new Intent(this, CalcResultActivity.class);
            startActivity(intent);
    }



}

and here's the xml file of main activity: 这是主要活动的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:text="@string/welcome_message" />  
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_the_dates"
        android:onClick="intent1"  />
</LinearLayout>

and this is what the actionbar look like when I run the app: http://i.imgur.com/jADDz7b.png 这是我运行应用程序时操作栏的外观: http ://i.imgur.com/jADDz7b.png

The app doesn't show any errors, and the icon is put in the right way in the directories. 该应用程序未显示任何错误,并且该图标已正确放置在目录中。 so can anyone tell me what's the problem and how can I fix it? 所以谁能告诉我问题出在哪里,我该如何解决?

The problem with @ZsoltBoldizsár's answer is that you are using ActionBarActivity . @ZsoltBoldizsár回答的问题是您正在使用ActionBarActivity Change getActionBar() to getSupportActionBar() getActionBar()更改为getSupportActionBar()

Add the following code in onCreate(Bundle) after you call super.onCreate(Bundle) : 在调用super.onCreate(Bundle)之后,在onCreate(Bundle)添加以下代码:

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);

Appcompat-v7 automatically disables setDisplayShowHomeEnabled so you must set it to true to show an icon. Appcompat-v7自动禁用setDisplayShowHomeEnabled,因此必须将其设置为true才能显示图标。

You need to configure the actionbar. 您需要配置操作栏。 Read the documentation , but the line below might help you. 阅读文档 ,但是下面的行可能会对您有所帮助。

getActionBar().setHomeButtonEnabled(true);
getActionBar().setIcon(here comes your drawable id)  (eg. R.drawable.ic_launcher)

Try with this: 试试这个:

ActionBar ab = getActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowCustomEnabled(true);
ab.setTitle(Html.fromHtml("your text"));
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor(your_colorCode));
ab.show();

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

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