繁体   English   中英

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

[英]Toolbar icons not appearing in AppCompat Activity

我正在尝试将工具栏添加到我的应用程序,但不知何故。 既没有标题也没有动作图标出现。 即使“:”菜单也没有出现。 这是我的代码:

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>

输出截图:

输出截图:

有帮助吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM