【问题标题】:Android CoordinatorLayout shows duplicated App BarAndroid CoordinatorLayout 显示重复的 App Bar
【发布时间】:2016-11-28 21:14:45
【问题描述】:

基本上我用 CoordinatorLayout 创建了一个 Activity,一切似乎都在工作。但是有 2 个 AppBar。一个是空的(无文本)并显示在活动的顶部。而另一个在协调器布局上表现得应该:最初只是文本,没有背景;当您向上滑动时,它会移动到屏幕顶部,并获得一个背景(如果这听起来令人困惑,请查看动画here

这是它的外观截图:

我尝试使用:

requestWindowFeature(Window.FEATURE_NO_TITLE);

supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

但似乎两者都对布局没有任何影响。

这是我的活动 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="aris.projectaris.InfoPreviewActivity">

    <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/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_info_preview" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

【问题讨论】:

  • hierarchyviewer / layout inspector 显示什么?
  • 好吧,它在顶部显示了一个工具栏。我很确定它应该在那里(非空的)。我用我的活动 xml 文件更新了问题

标签: android android-layout android-design-library androiddesignsupport


【解决方案1】:

看起来默认的ActionBar 仍然存在,即使您添加了Toolbar。确保设置以下主题

android:theme="@style/Theme.AppCompat.NoActionBar"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"

&lt;application&gt;AndroidManifest.xml 中的适当&lt;activity&gt; 标记中。

编辑:

如果您使用这些主题之一,则必须在 Activity 的onCreate 方法中将 Toolbar 设置为应用栏:

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

    Toolbar t = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(t);
}

【讨论】:

  • 是的。那讲得通。然而,它不起作用。主题似乎只改变了 both 栏的背景颜色,但它们仍然存在。
  • 你认为获取ActionBar View 是可能的吗?也许将其可见性设置为 View.GONE 可行。
  • 无论如何,你为什么不使用工具栏周围的CollapsingToolbarLayout
  • 可以在Activity中使用getActionBar()获取ActionBar的引用。
  • “工具栏周围”是什么意思?
猜你喜欢
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-22
  • 2020-10-12
  • 2018-02-26
  • 1970-01-01
  • 2018-09-17
相关资源
最近更新 更多