AppCompatActivity as a dialog without title

China☆狼群 提交于 2019-12-18 10:58:35

问题


I have an Activity inherited from AppCompactActivity. in manifest for activity set theme:

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

When I run activity, it shows as a dialog, but title is shown! i try supportRequestWindowFeature(Window.FEATURE_NO_TITLE) and RequestWindowFeature(Window.FEATURE_NO_TITLE) but title still displayed. Please let me know, What is wrong?


Edit

I solve it, only change android:windowNoTitle to windowNoTitle! because i am use AppCompactActvity!


回答1:


If you are having AppCompatActivity then the following won't work

requestWindowFeature(Window.FEATURE_NO_TITLE);

The simple way is to set it in the style.xml file.

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

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

If you want to remove it programmatically then add the following in onCreate()

getSupportActionBar().hide();



回答2:


AppCompatActivity is different than Activity and has its own features. For the same purpose, you can simply use -

supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

You can look up the documentation here

Note: Add this before setContentView() to avoid crash.




回答3:


Set below style in your style.xml

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

than set this theme in your activity

<activity
    android:name=".yourDailogActivity"
    android:configChanges="orientation"
    **android:theme="@style/CheckoutDialogTheme"**
    android:screenOrientation="portrait" />



回答4:


Should keep nothing over Activity title. if following these steps activity title will be hidden.

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:

after setContentView(R.layout.xyz) like this

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



回答5:


please use the request window feature before setContentView() as below

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



回答6:


Should keep nothing over <string name="app_name"></string>.so when you run your activity it will show dialog with "NO Title".

string.xml:

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


来源:https://stackoverflow.com/questions/30184031/appcompatactivity-as-a-dialog-without-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!