工具栏无法按预期在android中自定义

时间:2015-11-12 17:31:13

标签: java android xml material-design

我从slidenerd提供的视频教程中学习Material Design。他们在工具栏上放了一个下一个图标,一切都在该教程中工作,但是当我实现它时,图标没有出现。< / p>

在这里,我附上我的文件,请在我的学习阶段看看并帮助我。请!!!!

MainActivity.java

package materialtest.example.ritzipsy.mytoolbar5;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
    private Toolbar toolbar;

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

        // Enable the Up button
        ab.setDisplayHomeAsUpEnabled(true);

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.id_settings) {
            Toast.makeText(this, "You have Clicked Setting menu", Toast.LENGTH_SHORT).show();
        }
        if (id == R.id.id_navigate) {
            Toast.makeText(this, "You have Clicked Next menu", Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }
}

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<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="materialtest.example.ritzipsy.mytoolbar5.MainActivity">

    <include android:id="@+id/app_bar"
        layout="@layout/app_bar"></include>
</RelativeLayout>

my_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/tools">
    <item
        android:id="@+id/id_settings"
        android:title="Settings"
        app:showAsAction="never">></item>
    <item
        android:id="@+id/id_navigate"
        android:icon="@drawable/ic_next_arrow"
        android:orderInCategory="200"
        android:title="Next"
        app:showAsAction="always"></item>

</menu>

app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.co0m/apk/res-auto"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary">

</android.support.v7.widget.Toolbar>

style.xml

<resources>

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

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

</style>
</resources>

输出为

enter image description here

预期输出为

enter image description here

1 个答案:

答案 0 :(得分:1)

只需更改菜单xml,如下所示。问题出在这条线上 的xmlns:应用程式=&#34; HTTP://schemas.android.com/apk/tools"

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/id_settings"
        android:title="Settings"
        app:showAsAction="never"></item>
    <item
        android:id="@+id/id_navigate"
        android:icon="@drawable/ic_next_arrow"
        android:orderInCategory="200"
        android:title="Next"
        app:showAsAction="always"></item>

</menu>