【问题标题】:Android Studio designer preview different to phoneAndroid Studio 设计器预览不同于手机
【发布时间】:2016-02-03 16:01:18
【问题描述】:

我是 android studio 新手,在 android 平台上进行开发。我遇到了一个问题,即 Android Studio 设计器预览没有准确显示我手机上的内容。它在屏幕顶部显示了一个额外的工具栏小部件。我查看了活动的 XML 文件,但找不到它的来源。如果您能提供帮助,我们将不胜感激。

设计师:http://imgur.com/A4TZFWb

电话:http://imgur.com/kxP4CFn

这是来自 XML 文件的 sn-p:

<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.tomnulty.helloworld.SecondActivity">

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

这是来自 AndroidManifest 文件的 sn-p:

 <activity
        android:name=".SecondActivity"
        android:label="@string/hello_world"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.SECOND" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

【问题讨论】:

  • 你用的是什么主题?在 AS 的预览窗格中,您会在顶部找到所选主题
  • @HrundiV.Bakshi 第一张图是Android Studio中Activity的预览图。第二张图片是我手机上的活动截图。在预览中,我有一个额外的工具栏小部件,其中包含“Hello World”。它不应该在那里,尽管我在 XML 文件中找不到它的创建位置。
  • @Pztar 我正在使用“AppTheme”。这是错的吗?
  • 不,不一定是错的。进入您的styles.xml 并在AppTheme 样式下将父级设置为Theme.AppCompat.Light.NoActionBar
  • 我看到您至少使用了 3 个主题...可能是某个主题与另一个主题冲突?

标签: android xml android-studio preview


【解决方案1】:

正如 Pztar 所说,“进入您的 styles.xml 并在 AppTheme 样式下将父级设置为 Theme.AppCompat.Light.NoActionBar

虽然我在AndroidManifest.xml 中声明活动时设置了我的主题,但这并没有改变预览。这必须通过更改 Android Studio 的 UI 或 styles.xml 文件中的主题来完成。

样式.xml:

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

【讨论】:

  • 这是因为在您的清单中,您将活动主题设置为AppTheme.NoActionBar,而您的预览版默认有AppTheme,它包含ActionBar/ToolBar,因此当您指定.NoActionBar 时,它会将其删除。您现在可以将您的清单设置为 AppTheme,因为您覆盖了父值,它会在没有栏的情况下显示它。
【解决方案2】:

使用 Android Studio 3.0 stable,您可以在预览面板中更改为您的自定义主题。 提醒一下,如果您在 AndroidManifest.xml 中的 Activity 没有实现相同的设计时间,则设计时间与运行时不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-06
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 2019-01-27
    • 2017-03-20
    • 1970-01-01
    相关资源
    最近更新 更多