当将布局包含到协调 View 中时,android Studio 布局预览为空

标签 android android-studio android-coordinatorlayout coordinator-layout

在我发布这个问题之后,它没有提供足够的细节,现在我已经到了问题发生的地步。所以我将我的问题编辑为下面的特定方式

已编辑

您好,我已经尝试了很多可能的解决方案,但预览布局没有给出渲染错误。

它给出了 coordinatorLayout 的错误。它实际上显示了协调器布局的预览,但是当我使用

包含另一个布局时它什么也没显示
<include layout="@layout/content_main" />

进入协调器 View

我尝试清理和构建项目,我尝试将应用程序样式更改为下面的样式

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

我已将预览 api 从 22 更改为其他

但没有任何作用。

虽然它会预览其他布局,但不会在包含的布局和协调器布局中进行预览。当我评论包含标记行时,它会显示其中的内容。

那么我要用这个 CoordinatorView 和包含的布局做什么?

我不知道!!我有 android studio 1.5.1,我只是创建了一个空白 Activity 而不是空 Activity ,然后它会自动创建这些布局。不知道为什么

这是两种布局的屏幕截图。 enter image description here enter image description here

这是代码 内容布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.algtek.magnetotest.MainActivity"
tools:showIn="@layout/activity_main">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
</RelativeLayout>

协调器布局

<?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.algtek.magnetotest.MainActivity">

<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_main" />

<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"
    android:src="@android:drawable/ic_dialog_email" />

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

最佳答案

我在 Android Studio 2.1.1 中遇到了同样的错误,并设法通过调整 include_view 的内容来解决它。

我不知道为什么但是空预览来自属性:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

诀窍是在主布局中使用此属性,并为内容包含一个布局。这样一切都按预期工作......

这是你应该拥有的。

Activity 布局:

<?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.algtek.magnetotest.MainActivity">

<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>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

</android.support.v4.widget.NestedScrollView>

<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"
    android:src="@android:drawable/ic_dialog_email" />

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

content_main.xml :

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
</RelativeLayout>

不要忘记删除包含的 xml 中的 tools:showIn。它似乎使预览中断(可能是因为它试图呈现 app:layout_behavior)。

我冒昧地将 NestedScrollView 添加为 RelativeLayout 的父级,以便您可以利用 CoordinatorLayout。 如果您不想/不需要 NestedScrollView,请将其替换为您的 RelativeLayout 并使用合并标记作为 main_content.xml 的父级

关于当将布局包含到协调 View 中时,android Studio 布局预览为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34548126/

相关文章:

java - CoordinatorLayout + CollapsingToolbarLayout 向上滚动时不设置工具栏

android - flutter 中just_audio pugin的问题

android-studio - 谷歌播放服务不是最新的

android - 查找 “NdkCompile is no longer supported”的来源

android - 工具栏在 android 4.4 平板电脑中不可见

android - 具有多个捕捉点的 CoordinatorLayout

android - 显示许多标记 map v2 android 哪个性能更好?

java - 是否可以以编程方式将文件从 Android 智能手机发送到计算机/PC?

android - 即使应用程序未运行,也会在精确时间启动 alarmManager

安卓工作室 2.0 "waiting for the debugger to attach"