无法创建操作按钮

时间:2014-08-04 16:55:56

标签: android

我目前正在尝试学习android编程并遇到了问题。当我尝试完成Action Bar教程时,它不起作用。它运行,但在操作栏上只有溢出按钮显示并包含设置按钮。我不知道为什么这不起作用。这是代码:

package com.example.usingui;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

public final static String EXTRA_MESSAGE = "com.example.usingui.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    return super.onCreateOptionsMenu(menu);
}

@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 sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);

}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}
}

res/menu/main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      yourapp:showAsAction="ifRoom" />
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:showAsAction="never" />
</menu>

我不知道我做错了什么。请帮帮我!

3 个答案:

答案 0 :(得分:1)

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

        case R.id.action_settings:
            //Do something here
            return true;

        case R.id.action_search:
            //do something here
            return true;



        default:
        return super.onOptionsItemSelected(item);
        }
    }

这是我操作栏的代码

答案 1 :(得分:0)

我在你的XML菜单中看到,你有android:showAsAction =“never”将它总是改为你想要在操作栏中看到的每个菜单项,所以就是这样:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="always" />

<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:showAsAction="always" />

</menu>

答案 2 :(得分:0)

@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; ->you can handle every item of action bar and you can do any interaction in here!
    }
    return super.onOptionsItemSelected(item);
}

我同意Cyber​​gei你应该通过写android:showAsAction="always"来设置每个动作栏的可见性,正如我上面所写,你可以通过处理{{1}中每个项目的id来做你想做的任何事情。方法