有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android ActionBar不见了

在我的项目main活动中。java是--

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) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main_actions, menu);

        return super.onCreateOptionsMenu(menu);
    }
}

res=>;菜单我有活动_主要动作。xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:安卓="http://schemas.安卓.com/apk/res/安卓" >
    <!-- Search / will display always -->
    <item 安卓:id="@+id/action_search"
          安卓:icon="@drawable/ic_action_search"
          安卓:title="@string/action_search"
          安卓:showAsAction="ifRoom"/>

    <!-- Location Found -->
    <item 安卓:id="@+id/action_location_found"
          安卓:icon="@drawable/ic_action_location_found"
          安卓:title="@string/action_location_found"
          安卓:showAsAction="ifRoom" />

    <!-- Refresh -->
    <item 安卓:id="@+id/action_refresh"
          安卓:icon="@drawable/ic_action_refresh"
          安卓:title="@string/action_refresh"
          安卓:showAsAction="ifRoom" />

    <!-- Help -->
    <item 安卓:id="@+id/action_help"
          安卓:icon="@drawable/ic_action_help"
          安卓:title="@string/action_help"
          安卓:showAsAction="never"/>

    <!-- Check updates -->
    <item 安卓:id="@+id/action_check_updates"
          安卓:icon="@drawable/ic_action_refresh"
          安卓:title="@string/action_check_updates"
          安卓:showAsAction="never" />
</menu>

我的价值观=>;风格。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.DarkActionBar">
        <!--
            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>

还有我的缺陷。xml是:--

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

    <uses-sdk
        安卓:minSdkVersion="11"
        安卓:targetSdkVersion="21" />

    <application
        安卓:allowBackup="true"
        安卓:icon="@drawable/ic_launcher"
        安卓:label="@string/app_name"
        安卓:theme="@style/AppTheme" >
        <activity
            安卓:name=".MainActivity"
            安卓:label="@string/app_name" >
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

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

</manifest>

在我的布局文件夹中,我有活动_main。xml即:-

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:paddingBottom="@dimen/activity_vertical_margin"
    安卓:paddingLeft="@dimen/activity_horizontal_margin"
    安卓:paddingRight="@dimen/activity_horizontal_margin"
    安卓:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.actionbar_example.MainActivity" >

    <TextView
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:text="@string/hello_world" />

</RelativeLayout>

当我在eclipse图形视图中打开这个lauout时,它会显示带有项目的actionBar。。但是当我在我的安卓手机上运行这个程序时,动作栏不见了。。输出为白色窗口,没有操作栏,只显示“hello world”。。为什么我不能打开ActionBar窗口???问题出在哪里


共 (2) 个答案

  1. # 1 楼答案

    改变

    public class MainActivity extends Activity{
    

    public class MainActivity extends ActionBarActivity{
    

    并导入所需的类

  2. # 2 楼答案

    用^{扩展你的活动

    支持Android 2.1及以上版本

    在比Android 3.0(下至Android 2.1)旧的版本上运行时,添加操作栏需要在应用程序中包含Android支持库

    要开始,请阅读Support Library Setup文档并设置v7 appcompat库(下载库软件包后,请按照说明使用资源添加库)

    将支持库与应用程序项目集成后:

    更新活动,使其扩展ActionBarActivity。例如:

    public class MainActivity extends ActionBarActivity { ... }
    

    在清单文件中,更新<application>元素或单个<activity>元素以使用其中一个主题。AppCompat主题。例如:

    <activity android:theme="@style/Theme.AppCompat.Light" ... >
    

    注意:如果您创建了自定义主题,请确保它使用其中一个主题。AppCompat主题作为其父主题。有关详细信息,请参见设置动作栏的样式。 现在,在Android 2.1(API级别7)或更高版本上运行时,您的活动包括操作栏

    请记住在清单中正确设置应用程序的API级别支持:

    <manifest ... >
        <uses-sdk android:minSdkVersion="7"  android:targetSdkVersion="18" />
        ...
    </manifest>
    

    source