Android活动作为对话框,但没有标题栏

问题描述

| 我有一个具有以下主题设置的活动:
android:theme=\"@android:style/Theme.Dialog\"
但是,活动对话框中会出现一个标题栏,该标题栏占用了我的少量可用空间。如何删除?     

解决方法

        尝试在
onCreate
中做
requestWindowFeature(Window.FEATURE_NO_TITLE);
。必须在调用
super.onCreate
之后并在
setContentView
之前立即完成。     ,        对于正在使用ѭ5those的用户,上述答案可能无效。 尝试这个
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.MyDialogActivity);
之前     ,        您可以定义自己的主题:
<style name=\"NoTitleDialog\" parent=\"@android:style/Theme.Dialog\">
    <item name=\"android:windowNoTitle\">true</item>
</style>
(@Rehan的提示)如果使用的是兼容性库,则应选择
AppCompat
父主题,并省略
android:
前缀。例如:
<style name=\"NoTitleDialog\" parent=\"@style/Theme.AppCompat.Dialog\">
    <item name=\"windowNoTitle\">true</item>
</style>
    ,        如果使用的是
AppCompatActivity
,其中所有内容都必须使用
Theme.AppCompat.xxx
,则可以使用以下方法删除
styles.xml
中的对话框标题或操作栏:
<style name=\"Theme.MyDialog\" parent=\"Theme.AppCompat.Light.Dialog\">
    <item name=\"windowNoTitle\">true</item>
    <item name=\"windowActionBar\">false</item>
</style>
注意使用the16代替instead17。     ,        如果您使用的是AppCompatActivity,则requestWindowFeature(Window.FEATURE_NO_TITLE)无法正常工作。 为此,您可以在values / style.xml中创建自定义主题,如下所示:
   <style name=\"NoTitleDialog\" parent=\"Theme.AppCompat.Dialog.Alert\">
        <item name=\"windowNoTitle\">true</item>
    </style>
请注意,项目名称:\“ windowNoTitle \”不是 项目名称:\“ android:windowNoTitle \” 在menifest.xml中,将此自定义主题应用为
<activity android:name=\".activity.YourActivity\"
            android:theme=\"@style/NoTitleDialog\"/>
或者您可以使用getSupportActionBar()。hide()以编程方式隐藏操作栏。     ,        这个为我的appcompatpat工作。
<style name=\"DialogTheme\" parent=\"Theme.AppCompat.Light.Dialog\">
    <item name=\"colorPrimary\">@color/colorPrimary</item>
    <item name=\"colorPrimaryDark\">@color/colorPrimaryDark</item>
    <item name=\"colorAccent\">@color/colorAccent</item>
    <item name=\"windowActionBar\">false</item>
    <item name=\"windowNoTitle\">true</item>
</style>
    ,        对于对话框,您可以这样做:
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    ,        这个为我工作。 我可以禁用标题 style.xml
<style name=\"AppDialogScreenTheme\" parent=\"Base.Theme.MaterialComponents.Light.Dialog\">
    <item name=\"windowActionBar\">false</item>
    <item name=\"windowNoTitle\">true</item>
</style>
AndroidManifest.xml
  <activity
        android:name=\".MyActivity\"
        android:theme=\"@style/AppDialogScreenTheme\">

  </activity>
    ,        这对我有用,也可能对某人有帮助: style.xml
<style name=\"NoTitleActivityDialog\" parent=\"Theme.AppCompat.Light.Dialog.Alert\">
    <item name=\"windowNoTitle\">true</item>
</style>
AndroidManifest.xml
   <activity
        android:name=\".DialogActivity\"
        android:theme=\"@style/NoTitleActivityDialog\"/>