工具栏图标未出现在AppCompat活动中

时间:2016-04-03 08:26:14

标签: java android xml

我正在尝试将工具栏添加到我的应用程序,但不知何故它无法正常工作。标题不是动作图标都出现了。甚至“:”菜单也没有出现。这是我的代码:

Style.xml:

<resources>

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

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


    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

Menu_main.xml:

<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:context="com.example.vaibhav.thirdeye.MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />

    <item android:id="@+id/next"
        android:title="@string/next"
        android:orderInCategory="200"
        android:icon="@drawable/next_arrow"
        app:showAsAction="always"/>
</menu>

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back_splash"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.vaibhav.thirdeye.MainActivity"
    tools:showIn="@layout/activity_main"
    app:theme="@style/AppTheme.NoActionBar">

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

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    public static final String EXTRA_MESSAGE = "com.example.vaibhav.thirdeye.MESSAGE";
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //Adding Custom toolbar
        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.next_arrow);
        toolbar.setTitle("ThirdEye");
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        switch (id){
            case R.id.action_settings:
                Toast settings = Toast.makeText(this, "Voila!", Toast.LENGTH_SHORT);
                settings.show();
            case R.id.next:
                Toast next = Toast.makeText(this, "Voila!", Toast.LENGTH_SHORT);
                next.show();
        }
        return true;
    }
}

app_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/icon_size"
    android:background="@color/colorPrimary">

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

输出截图:

Output Screenshot:

任何帮助????

1 个答案:

答案 0 :(得分:0)

删除MainActivity的activity_main.xml布局中的额外工具栏,它将解决您的问题。