【问题标题】:Menu doesn't appear in custom toolbar layout菜单未出现在自定义工具栏布局中
【发布时间】:2020-06-03 06:06:42
【问题描述】:

我看过一个教程视频,该人为工具栏创建自定义布局并将 manu 设置为运行良好的工具栏蚂蚁 我做了同样的事情,但菜单没有出现在我的自定义工具栏中! 这是我的代码。你知道问题出在哪里吗?

这是工具栏布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:minWidth="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_height="wrap_content">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/txtCounter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0 Item Selected"
        android:textSize="18sp"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.appcompat.widget.Toolbar>

这是主要的活动布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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"
tools:context=".MainActivity">

<include
    android:id="@+id/include"
    layout="@layout/toolbar_layout"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />



 </androidx.constraintlayout.widget.ConstraintLayout>

这是我处理工具栏和菜单的主要活动

public class MainActivity extends AppCompatActivity {

Toolbar toolbar ;

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

    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


}

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

【问题讨论】:

  • &lt;include&gt; 上的android:id 会覆盖包含布局中根View 上的android:id,因此您的Toolbar 以ID include 结束。这意味着 findViewById(R.id.toolbar) 返回 null,因此没有设置任何内容作为 ActionBar,并且永远不会设置菜单。要么删除 &lt;include&gt; 上的 android:id,要么将 findViewById() 调用改为传递 R.id.include
  • 它有效。我删除了包括 id 。 tnx

标签: android


【解决方案1】:

您在 findViewByID() 中调用它时使用了错误的 ID。请确保您只需要一个。像这样进行更改。

在您的主要活动布局中

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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"
tools:context=".MainActivity">

<include
layout="@layout/toolbar_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

在您的主要活动课程中

    toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    相关资源
    最近更新 更多