Android Studio:活动预览未显示标题栏。

8

我知道这个问题之前已经有人提出过(这里),但我尝试了给出的解决方案,但预览效果与设备的完全一样。

我使用的是安卓版本4.2.2的三星Galaxy S3 mini作为我的设备调试,它完全没有改动,但我不明白为什么即使我更改主题,设备看起来仍然一样(当然在活动中做出了更改)。

活动的代码就像任何新的空白活动一样。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 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" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

预览看起来像这样: 输入图像描述 但是设备看起来像这样: 输入图像描述 PS:我正在学习安卓,所以可能会对答案感到困惑。
5个回答

30

我有类似的问题。在我的情况下,这只是设计视图的配置问题。当我标记“显示布局装饰”时,它就如预期一样出现了。Show Layout Decorations


1
  1. 看起来你在 styles.xml 文件中定义了一个主题,这导致你的模拟器显示上述输出。

  2. 在活动的设计视图中从 AppTheme 选项中选择与 styles.xml 中定义的相同的主题,以便显示你的 ActionBar。

enter image description here


这在文件中是如何定义的?当我选择相同的主题时,它没有改变。 - Oscar Reyes
@OscarReyes 我知道这是很久之前的事情了,但你解决了这个问题吗? - Devealte

1
为了使您的活动自动显示标题栏,您需要将活动扩展到AppCompatActivity而不是Activity。并且请确保在您的manifest.xml中没有将主题设置为。
android:theme="@style/AppTheme.NoActionBar"

但是

android:theme="@style/AppTheme"  

1

我有同样的问题。我的问题是在我的styles.xml文件中有以下内容:

<item name="windowActionBar">true</item>
<item name="windowNoTitle">true</item>

enter image description here

将其更改为此后,标题栏再次显示出来。

<item name="windowActionBar">true</item>
<item name="windowNoTitle">false</item>

enter image description here


0
只需前往 app/scr/main/res/values/styles.xml 并将您的 AppTheme 样式更改为 Theme.AppCompat.Light.NoActionBar
   <!-- Base application theme. -->
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接