You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio模拟器中设计不可见,提示需添加@Preview求指导

解决Android Studio XML布局预览不显示的问题

你用的是传统XML布局,@Preview注解是Jetpack Compose专属的,XML布局根本不需要加这个。预览不显示的问题,按下面步骤排查:

  • 切换到Design视图:打开你的XML文件后,编辑器右上角有「Design」按钮(和「Code」并排),点击即可切换到可视化预览界面。
  • 调出预览面板:如果右侧没看到预览区域,点击右侧边栏的「Preview」标签,或者通过顶部菜单栏「View > Tool Windows > Preview」打开。
  • 修复同步或缓存问题:要是预览还是不加载,先点顶部工具栏的「Sync Project with Gradle Files」(大象图标)同步项目;如果无效,就选「File > Invalidate Caches...」,点击「Invalidate and Restart」重启Android Studio。
  • 可选优化预览:可以给控件添加tools命名空间的属性增强预览效果,比如给TextView加tools:text="预览显示文本",这个属性只在预览中生效,不影响实际运行。

你的XML布局代码本身没问题,贴出来参考:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity2">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="xd"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

内容的提问来源于stack exchange,提问作者raquel Fernandez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 14:53:25