无法在Android操作栏中添加两个按钮

时间:2015-09-02 12:59:11

标签: android android-actionbar

我很新。我想显示两个按钮并更改他们的图标,但我失败了。 我不明白这个问题。

You can also use "always" to declare that an item always appear as an action button. 我读了它并且一直使用但它没有'工作

menu_mainxml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:icon="@drawable/ic_action_toggle_setting"
    myapp:showAsAction="always"/>

<item android:id="@+id/action_about"
    android:title="@string/action_about"
    android:icon="@drawable/ic_action_toggle_about"
    myapp:showAsAction="always"/>

MainActivityjava:

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;

public class MainActivity extends Activity {
private MenuItem item1;

@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.menu_main, menu);
    item1= menu.findItem(R.id.action_about);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {

     //some code
        return true;
    }
    if (id == R.id.action_about) {

    //some code
        return true;
    }

    return super.onOptionsItemSelected(item);
}}

我的结果: enter image description here

3 个答案:

答案 0 :(得分:0)

更改活动中的以下内容:

private MenuItem item1;



     @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            item1= menu.findItem(R.id.action_about);

            return true;
        }

答案 1 :(得分:0)

尝试使用AppCompatActivity而不是Activity。

MainActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@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.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);
}
}

menu_main

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

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

<item android:id="@+id/action_about"
    android:title="@string/action_about"
    android:icon="@drawable/ic_action_toggle_about"
    myapp:showAsAction="always"/>

答案 2 :(得分:0)

你应该使用下面的代码并删除myapp prefrex而不是使用下面的android

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

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

</menu>