【问题标题】:android Studio layout preview is empty when include layout into the coordinatior view将布局包含到协调视图中时,android Studio布局预览为空
【发布时间】:2016-04-05 12:37:30
【问题描述】:

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

已编辑

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

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

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

进入协调器视图

我已尝试清理和构建项目,已尝试将应用样式更改为以下样式

<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 更改为其他

但没有任何效果。

虽然它会预览其他布局,但它不会在包含的布局和 coordinatorlayout 中都这样做。当我评论包含标签行时,它会显示其中的内容。

那么我在用这个协调视图和包含的布局做什么??

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

这是两种布局的屏幕截图。

这里是代码 内容布局

<?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 版本是多少?
  • 您是否安装了所有必要的 SDK?
  • 感谢您的帮助@Coeus 版本是 1.5.1
  • 感谢您的帮助@Sajib 我需要哪个sdk?我正在添加一些来自 SDK Manager 的屏幕截图
  • 你能试试 File > Invalidate caches and restart 吗?

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


【解决方案1】:

活动布局 XML 看起来不错。问题可能出在包含的 content_main 布局中。打开它,看看它是否首先呈现良好。还要在此处发布该布局的 XML。它是否添加了任何视图?他们应该展示。如果没有,请尝试添加一些,然后检查渲染是否适用于活动布局。

【讨论】:

    【解决方案2】:

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

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

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    

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

    这是你应该拥有的。

    活动布局:

    <?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 的父级

    【讨论】:

      猜你喜欢
      • 2018-04-23
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多