操作栏不会消失。

3
我刚开始学习Android编程,想要禁用主活动上方默认的操作栏,并替换为可自定义的工具栏。我看了这个网页并按照说明进行操作:https://developer.android.com/training/appbar/setting-up 不幸的是,操作栏并没有消失。尽管工具栏被显示出来了,但操作栏仍然留在活动布局的上方。
我将此代码插入了清单文件和布局文件中:
application android:theme="@style/Theme.AppCompat.Light.NoActionBar"
如果我将它插入到布局文件中,新创建的工具栏就变成了透明的,这不是我想要的。我希望默认的操作栏消失。
如果我将其插入到清单中,则不会发生任何事情。
以下是布局文件的xml代码的一部分:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"

    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="331dp"
        android:layout_height="62dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.018"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

如果有人能帮我解决这个问题,我会非常开心。提前谢谢!

2个回答

3

在你的 styles.xml 文件中:

:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

改为

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

现在你可以随意使用你的工具栏了。

希望这个方法能解决你的问题...祝你好运!!!


谢谢DaveAAA的回答。它对我很有帮助:-) 我在想为什么开发者网站上没有提到这个。 - VanessaF
手机工具栏和状态信息栏之间仍然存在一个小的空白区域。我该如何消除它?我希望新的工具栏紧接在手机状态信息栏之后开始。目前,在其下方有一个白色的空白区域。 - VanessaF

1

修改 styles.xml 文件并使用一个从 AppCompat 继承的 NoActionBar 主题。至于工具栏和状态栏之间的白色空间,您可以通过移除工具栏的 margin_top 属性来修复它。建议在不同屏幕尺寸之间使用 ?attr/actionBarSize 来设置工具栏的高度。工具栏应该与其父容器的宽度匹配,之间没有间距。下面是更新后的代码。

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.018"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

感谢Rafsanjani的帮助。属性“?attr/actionBarSize”是做什么用的?我在我的示例中使用了dp进行大小调整。我认为这是设备无关的。 - VanessaF

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接