【问题标题】:Toolbar - findViewbyID returning null工具栏 - findViewbyID 返回 null
【发布时间】:2014-12-14 12:19:55
【问题描述】:

为什么在我的布局中找不到我的Toolbar?

setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
}

之后,工具栏仍然为空。

activity_man.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    tools.context=".PlayerActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>
    <ImageView
        android:id="@+id/background_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>
        : (goes on)

编辑:

工具栏.xml:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"/>

谢谢!

【问题讨论】:

  • res/layout/toolbar.xml 中有什么内容? R.id.toolbar 指向 &lt;include&gt;,而不是 Toolbar。 “在我的工具栏布局中只有工具栏”——但这个布局不是你的问题的一部分,所以我们看不到它的内容。
  • 您是否在使用setContentView 用于toolbar.xml?
  • 更新了 setcontenview 和工具栏布局。我玩了一会儿,现在它可以工作了,但工具栏在屏幕上不可见?!

标签: android toolbar android-5.0-lollipop


【解决方案1】:

您的代码运行良好,您可能没有禁用默认操作栏:

在styles.xml中更改以下内容以删除操作栏

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">

    <item name="windowActionBar">false</item>

</style>

使用toolbar.bringToFront();将工具栏置于最前面

【讨论】:

  • 已删除!我猜@+id/background_image ImageView 正在阻止工具栏显示,但我不知道如何解决。
  • 试试这个方法toolbar.bringToFront();
猜你喜欢
  • 2015-11-17
  • 1970-01-01
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多