【发布时间】: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指向<include>,而不是Toolbar。 “在我的工具栏布局中只有工具栏”——但这个布局不是你的问题的一部分,所以我们看不到它的内容。 -
您是否在使用
setContentView用于toolbar.xml? -
更新了 setcontenview 和工具栏布局。我玩了一会儿,现在它可以工作了,但工具栏在屏幕上不可见?!
标签: android toolbar android-5.0-lollipop