AppCompatActivity 作为没有标题的对话框

AppCompatActivity as a dialog without title

我有一个 Activity 继承自 AppCompactActivity。在清单中 activity 设置主题:

<style name="Theme.custom" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="colorButtonNormal">@color/accent</item>
    <item name="android:buttonStyle">@style/ButtonStyle</item>
</style>

当我运行activity时,它显示为对话框,但显示标题!我尝试 supportRequestWindowFeature(Window.FEATURE_NO_TITLE)RequestWindowFeature(Window.FEATURE_NO_TITLE) 但标题仍然显示。请告诉我,有什么问题吗?


编辑

我解决了,只把android:windowNoTitle改成windowNoTitle!因为我正在使用 AppCompactActvity!

请在setContentView()之前使用请求window功能,如下

requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);

不应在 <string name="app_name"></string> 上保留任何内容。因此,当您 运行 您的 activity 时,它将显示与 "NO Title" 的对话框。

string.xml:

         <resources>
               <string name="app_name"></string>
               <string name="hello_world">Hello world!</string>
               <string name="action_settings">Settings</string>
        </resources>

如果您有 AppCompatActivity 那么以下将不起作用

requestWindowFeature(Window.FEATURE_NO_TITLE);

简单的方法是在style.xml文件中设置。

<style name="mytheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="windowNoTitle">true</item>
</style>

It is name="windowNoTitle", not name="android:windowNoTitle"

如果您想以编程方式删除它,请在 onCreate()

中添加以下内容
getSupportActionBar().hide();

在您的 style.xml

中设置以下样式
<style name="customDialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="windowNoTitle">true</item>
</style>

然后在您的 activity

中设置这个主题
<activity
    android:name=".yourDailogActivity"
    android:configChanges="orientation"
    **android:theme="@style/customDialogTheme"**
    android:screenOrientation="portrait" />

不应保留任何超过 Activity 标题的内容。如果遵循这些步骤 activity 标题将被隐藏。

style.xml:

    </style>
        <style name="MyTitledActivityDialogTheme" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

manifiest.xml:

 <activity
            android:name="YouActivity" 
            android:theme="@style/MyTitledActivityDialogTheme" />

YourClass.java:

像这样setContentView(R.layout.xyz)之后

  setContentView(R.layout.xyz);
  if (getSupportActionBar() != null)
      getSupportActionBar().hide();

A​​ppCompatActivity 与 Activity 不同,有其自身的特点。出于同样的目的,您可以简单地使用 -

supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

你可以查一下文档here

注意:在 setContentView() 之前添加此内容以避免崩溃。