java - 缺少 Android 操作栏和菜单选项

标签 java android xml android-layout android-studio

enter image description here

我很难显示应该位于顶部的操作栏。每次我使用 ListView 时,顶部带有所有菜单选项的 ActionBar 都会在我身上消失。有人可以阐明为什么 ListView 会发生这种情况。

主要 Activity 类:

public class MainActivity extends Activity {

    ListView listView;
    ArrayAdapter<String> adapter;

    String[] android_versions = {"Cupcake",
            "Donut",
            "Eclair",
            "Froyo",
            "Gingerbread",
            "Honeycomb",
            "Ice Cream Sandwich",
            "Jelly Bean",
            "KitKat",
            "Lollipop"};

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

        listView = (ListView) findViewById(R.id.listView);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android_versions);
        listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

list 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.farrellequipment.www.listview" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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>

布局activity_main.xml文件:

<RelativeLayout 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" tools:context=".MainActivity">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView"
            android:layout_gravity="left|top" />
</RelativeLayout>

菜单menu_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=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

样式.xml文件

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

</resources>

这是一个简单直接的应用程序,但我缺少此应用程序所需的 ActionBar 和菜单选项。再次有人可以阐明这个问题。我已经搜索这个问题大约 4 个小时了,我需要帮助。提前谢谢你。

最佳答案

你只需要将Activity替换为AppCompatActivity

public class MainActivity extends AppCompatActivity {

ListView listView;
ArrayAdapter<String> adapter;

String[] android_versions = {"Cupcake",
        "Donut",
        "Eclair",
        "Froyo",
        "Gingerbread",
        "Honeycomb",
        "Ice Cream Sandwich",
        "Jelly Bean",
        "KitKat",
        "Lollipop"};

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

    listView = (ListView) findViewById(R.id.listView);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android_versions);
    listView.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

关于java - 缺少 Android 操作栏和菜单选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32337602/

相关文章:

java - 设置和获取方法未按计划工作

Java 骰子游戏生成新数字时遇到问题

java - 房间 - LiveData 未触发

java - 如何使用 Canvas 进行缩放

android - 如何使用 Android Studio 为 Android 实现 GCM Hello World

xml - 这种情况下的 Xpath 查询 --grouping values?

流模式下的 java.net.HttpRetryException : cannot retry due to server authentication,

java - 连接到模拟器服务器的多个模拟器客户端

java - 使用 XPath 查询搜索字符串的一部分

javascript - 是否可以在浏览器中从 json 创建 xml,然后下载该 xml?