【问题标题】:Why does Android Studio always show ActionBar in app design, even when disabled?为什么 Android Studio 总是在应用设计中显示 ActionBar,即使已禁用?
【发布时间】:2015-12-30 19:02:44
【问题描述】:

我在新的 Android Studio 1.4 中有一个应用程序(尽管这个问题在 1.3.2 中也存在),由于扩展功能,我决定使用工具栏而不是操作栏。

我已经相应地设置了 xml 和 java 以隐藏操作栏,并且在编译时它不存在,而是工具栏位于操作栏出现的顶部。但是在设计视图中我仍然看到它并且没有办法删除它。这让我很困扰,因为它隐藏了一些设计区域,而且我没有足够的空间来放置我的所有元素。

如何在 Android Studio 设计中移除 ActionBar (1) 并将其替换为工具栏 (2),以便 (3) 具有完整高度。

这是我的 xml 和 java 文件,看起来很多,但它们是 90% 的默认值。我在从 Theme.AppCompat.Light.DarkActionBar 派生的 styles.xml 中设置 AppTheme 主题,在清单中我告诉编译器使用 AppTheme.NoActionBar 作为活动主题。

activity_unlock.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="com.example.testapp.UnlockActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" 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="@android:color/white" app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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


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

content_unlock.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_unlock" tools:context="com.example.testapp.UnlockActivity">

</RelativeLayout>

UnlockActivity.java

package com.example.testapp;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class UnlockActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unlock);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

}

Android 清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testapp">

    <application>
        <activity android:name="com.example.testapp.UnlockActivity"
                    android:label="@string/title_activity_unlock"
                android:theme="@style/AppTheme.NoActionBar"
>
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
        </activity>
    </application>
</manifest>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:windowNoTitle">true</item>
        <!--We will be using the toolbar so no need to show ActionBar-->
        <item name="android:windowActionBar">false</item>
        <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->

    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

【问题讨论】:

    标签: java android xml android-layout android-studio


    【解决方案1】:

    最后这很容易。我刚把styles.xml

     <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    

    <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    

    我不知道为什么这会影响设计器视图,因为在启动时,两个版本都提供相同的布局而没有操作栏(因为我使用 AppTheme.NoActionBar,它在 styles.xml 中定义)我猜这是 Android Studio 设计渲染错误。

    【讨论】:

    • 我真的不会称它为错误。预览只是为了向您展示您的布局的样子。它只是懒得检查它是否应该显示操作栏,因为这不是它的目的。
    • 但我的布局将 windowActonBar 指定为 false,所以它应该在没有它的情况下呈现
    【解决方案2】:

    由于您可以通过多种方式以任何样式实例化布局,因此您可以通过单击如图所示的按钮来查看布局在不同样式中的外观。如果您未在代码中设置其他样式或主题,则将使用清单中的主题。

    【讨论】:

    • 死吧!如果我选择 AppTheme.NoActionBar,它会按预期工作。我刚打开这个选项的时候没看到这个选项,隐藏在项目主题下。
    • 但显然这就是它的外观。您仍然需要更改应用程序本身的答案中显示的主题以反映这种风格!
    猜你喜欢
    • 1970-01-01
    • 2012-03-13
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多