没有在Android中获得Action Bar

时间:2015-04-05 13:02:11

标签: android

我正在尝试在我的Android应用程序中使用ActionBar但它没有显示。经过可能的解决方案我从清单文件中删除了android:theme,现在ActionBar正在运行,但它显示的主题是全息黑暗。我无法获得全息灯主题。如果我将主题属性放在清单文件中,ActionBar就会消失。这是我的代码

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.widget.Toast;

  public class MainActivity extends Activity {

    @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) {
     switch (item.getItemId()) {
        // action with ID action_refresh was selected
        case R.id.action_refresh:
          Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT)
              .show();
          break;
        // action with ID action_settings was selected
        case R.id.action_settings:
          Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT)
              .show();
          break;
        default:
          break;
        }

        return true;
 }
  }
     main.xml



 <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.actionbar.MainActivity" >


    <item
    android:id="@+id/action_refresh"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:icon="@drawable/ic_action_refresh"
    android:title="Refresh"/>
<item
    android:id="@+id/action_settings"
    android:title="Settings">
</item>
 </menu>

   ****manifest.xml****


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

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

<application
    android:theme="@style/AppBaseTheme"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    >
    <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>
    </application>

    </manifest>



      Style.xml

  <resources>

    <!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

  </resources>

2 个答案:

答案 0 :(得分:2)

更正extends ActionBarActivity

答案 1 :(得分:0)

这里是您的问题的快速解决方案.....您可以在您的应用中应用您想要的任何主题,并且您只需在创建方法下方添加此2行代码即可显示操作栏像这样: -

    setContentView(R.layout.activity_main);
    ActionBar actionBar = getSupportActionBar();
        actionBar.show();