无法在Android Studio中添加操作栏项

时间:2016-01-04 21:41:14

标签: java android xml android-studio

我已经搜索了半个小时,其他人的解决方案都没有为我工作。

以下是右侧预览中显示的内容:

enter image description here

以下是我活动中的java代码(我删除了其他内容):

/

所有说明都说我在我的菜单中制作了一个XML。目录,但我没有,所以我做了(称为'菜单'在' res'内)。 XML' actionbaroverflow'有这个代码:

public class PlayGame extends AppCompatActivity {

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

    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_playgame);
    myToolbar.setTitle("ChessClock");
    myToolbar.setTitleTextColor(Color.WHITE);
    setSupportActionBar(myToolbar);
}

@Override
public void onBackPressed() { } //Back Button Disabled

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.actionbaroverflow, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_pause:
            Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT).show();
            break;
    }
    return true;
}

当我运行应用程序时,该项目根本没有显示。我有一个图标,但有人说它可能太大了,所以我只是把文字做成了文字而且还没有出现。

2 个答案:

答案 0 :(得分:0)

Tou需要添加到您的R.menu.actionbaroverflow

    android:icon="@drawable/your_icon"

会是这样的:

<item
    android:id="@+id/action_pause"
    android:icon="@drawable/your_icon"
    android:orderInCategory="100"
    android:title="Pause"
    app:showAsAction="always"/>

答案 1 :(得分:0)

这对我很有用:

<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:activity=".ProductsNewOrder">
    <item android:id="@+id/action_pause"
        android:title="Pause"
        android:visible="true"
        app:showAsAction="always"
        android:icon="@drawable/ic_pause"/>
</menu>

并在活动中:

setSupportActionBar(myToolbar);
if (myToolbar!= null) {
            myToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    int id = item.getItemId();
                    switch (id) {
                        case R.id.action_pause: {
                            //do something
                            break;
                        }
                    }
                    return true;
                }
            });
        }

我在菜单中充气:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.actionbaroverflow, menu);
        return true;
    }

希望它有所帮助!!!