【问题标题】:Custom support.v7.widget.Toolbar not showing on device自定义 support.v7.widget.Toolbar 未显示在设备上
【发布时间】:2015-11-05 01:16:34
【问题描述】:

如果我理解正确,这个小部件的目的是使工具栏与旧版本的 android(如 4.x)兼容,但由于某种原因,当我在具有 android 4.x 的实际设备上运行我的应用程序时,我没有看到我的工具栏。在带有 Android 6 的模拟器中一切正常。

我在我的主题中使用 @style/Theme.AppCompat.Light.NoActionBar,我的活动扩展了 AppCompatActivity

这是我的工具栏的样子:

<android.support.v7.widget.Toolbar
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/my_toolbar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="?attr/colorPrimary"
  android:elevation="4dp"
  android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

包含在我的活动 xml 中:

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

并在创建时添加到活动代码中:

toolbar = (Toolbar) findViewById(R.id.tool_bar);
this.setSupportActionBar(toolbar);

任何提示为什么这不会在 android 4.x 上显示?

【问题讨论】:

  • 如果父类是 AppCompat,请检查您正在使用的活动的默认样式/主题,如下所示
  • 你能说得更具体点吗?我不确定我是否理解你的意思。如果这就是您的意思,在我的主题样式中,我没有任何样式自定义。
  • 在你的清单上尝试放置这样的东西,即使你没有自定义主题样式.. android:theme="@style/Theme.AppCompat"
  • 同样的事情,但是我通过用 android.support.design.widget.AppBarLayout 包装我的工具栏来玩一点,而显示工具栏的唯一情况是当我包装我的 webview 控件时工具栏,但这很奇怪......
  • 天啊,我是多么愚蠢! :-/ 看来我变老了,晚上 10 点就困了! :) 我忘记将下面的内容包装到相对布局中,因此我的布局在旧版本上搞砸了,工具栏看起来很隐藏,但他实际上在那里。 :-/ 无论如何感谢您的帮助。

标签: android android-layout android-activity


【解决方案1】:

请试试这个。

将您的 XML 布局更改为。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context=".HomeActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

【讨论】:

  • 这里面有什么重要的东西??
【解决方案2】:

您正在选择一个不存在的工具栏 ID。放:

toolbar = (Toolbar) findViewById(R.id.my_toolbar);

改为。

【讨论】:

  • 我将它包含为 所以工具栏存在,它不为空,并且正如我所说,它适用于模拟器。
  • 试一试,让我知道结果。
  • 我在使用您的代码时遇到异常,正如我所说,工具栏与上面的代码存在。
  • 你在使用 AppBarLayout 吗?
  • 不,我没有。我有RelativeLayout,直接在里面我有这个包含块来从上面添加工具栏xml。
猜你喜欢
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多