如何在操作栏中设置操作栏图标

时间:2015-12-16 13:09:05

标签: android-actionbar android-menu

我是android新手,我想在操作栏中显示操作栏图标。简单的任务,但我无法得到这个。我上传了我的Mainactivity.java文件。

def after_inactive_sign_up_path_for(resource_or_scope)
   root_path
end

这是我的Custom Menu.XML文件

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

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

我还添加了我的Mainfest文件以供参考。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_search"
    android:icon="@drawable/ic_favorite_border_white_48dp"
    android:title="action_search"
    android:showAsAction="ifRoom"/>

<!-- Location Found -->
<item android:id="@+id/action_location_found"
    android:icon="@drawable/ic_star_rate_white_18dp"
    android:title="action_location_found"
    android:showAsAction="ifRoom" />

</menu>

我在Android Mobile(Nexus 5 Android 6.0.0)中运行该应用程序 谢谢,谢谢。

1 个答案:

答案 0 :(得分:0)

在您的主要活动中,您可以设置操作栏图标

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

    ActionBar actionBar = getSupportActionBar();
    actionBar.setIcon(R.drawable.ic_button);   /* setting icon */
}