Android AppCompat 的全屏主题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20653305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:13:10  来源:igfitidea点击:

Full Screen Theme for AppCompat

androidandroid-actionbarandroid-theme

提问by Ye Lin Aung

I would like to know how can I apply full screen theme ( no title bar + no actionbar ) to an activity. I am using AppCompat library from support package v7.

我想知道如何将全屏主题(无标题栏 + 无操作栏)应用于活动。我正在使用支持包 v7 中的 AppCompat 库。

I've tried to applied android:theme="@android:style/Theme.NoTitleBar.Fullscreen"to my specific activity but it crashed. I think it's because my application theme is like this.

我试图应用于android:theme="@android:style/Theme.NoTitleBar.Fullscreen"我的特定活动,但它崩溃了。我想是因为我的应用主题是这样的。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

I also have tried this

我也试过这个

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

which only hides title bar but not the action bar. My current workaround is that hiding the actionbar with

它只隐藏标题栏而不隐藏操作栏。我目前的解决方法是隐藏操作栏

getSupportActionBar().hide();

回答by nebyan

When you use Theme.AppCompat in your application you can use FullScreenTheme by adding the code below to styles.

当您在应用程序中使用 Theme.AppCompat 时,您可以通过将以下代码添加到样式来使用 FullScreenTheme。

<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

and also mention in your manifest file.

并在您的清单文件中提及。

<activity
   android:name=".activities.FullViewActivity"
   android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen" 
/>

回答by Hover Ruan

Based on the answer by @nebyan, I found that the action bar is still not hiding.

根据@nebyan 的回答,我发现操作栏仍然没有隐藏。

The following code works for me:

以下代码对我有用:

<style name="AppFullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

and of course dont forgot to edit your AndroidManifestfile.

当然不要忘记编辑您的AndroidManifest文件。

<activity
    android:name="YOUR_ACTIVITY_NAME"
    android:theme="@style/AppFullScreenTheme" 
/>

回答by Dharmendra Pratap Singh

<style name="Theme.AppCompat.Light.NoActionBar" parent="@style/Theme.AppCompat">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
</style>

Using the above xml in style.xml, you will be able to hide the title as well as action bar.

使用 style.xml 中的上述 xml,您将能够隐藏标题和操作栏。

回答by KrhashisM

Issues arise among before and after versions of Android 4.0 (API level 14).

在 Android 4.0(API 级别 14)之前和之后的版本中都会出现问题。

from hereI created my own solution.

这里我创建了自己的解决方案。

@SuppressLint("NewApi")
@Override
protected void onResume()
{
    super.onResume();

    if (Build.VERSION.SDK_INT < 16)
    {
        // Hide the status bar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // Hide the action bar
        getSupportActionBar().hide();
    }
    else
    {
        // Hide the status bar
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        / Hide the action bar
        getActionBar().hide();
    }
}

I write this code in onResume() method because if you exit from your app and then you reopen it, the action bar remains active! (and so this fix the problem)

我在 onResume() 方法中编写此代码,因为如果您退出应用程序然后重新打开它,操作栏将保持活动状态!(所以这解决了问题)

I hope it was helpful ;)

我希望它有帮助;)

回答by Simulant

Your "workaround" (hiding the actionBar yourself) is the normal way. But google recommands to always hide the ActionBar when the TitleBar is hidden. Have a look here: https://developer.android.com/training/system-ui/status.html

您的“解决方法”(自己隐藏 actionBar)是正常方式。但是谷歌建议在隐藏 TitleBar 时始终隐藏 ActionBar。看看这里:https: //developer.android.com/training/system-ui/status.html

回答by Shuyan Ji

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //to remove "information bar" above the action bar
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    //to remove the action bar (title bar)
    getSupportActionBar().hide();
}

回答by jagdish

You can follow below step:-

您可以按照以下步骤操作:-

AndoridMenifest.xml

AndoridMenifest.xml

<activity
            android:name=".ui.FullImageActivity"
            android:label="@string/title_activity_main"
            android:screenOrientation="landscape"
            android:theme="@style/DialogTheme">
        </activity>

Style.xml

样式文件

<style name="DialogTheme" parent="android:Theme.Dialog">

    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">fill_parent</item>

   <!-- No backgrounds, titles or window float -->
    <item name="android:windowNoTitle">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">false</item>
</style>

FullImageActivity.java

完整图像活动.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    setContentView(R.layout.image_view);
     }

I hope it will help..Thanks!!

我希望它会有所帮助..谢谢!

回答by vntstudy

It should be parent="@style/Theme.AppCompat.Light.NoActionBar"

它应该是parent="@style/Theme.AppCompat.Light.NoActionBar"

<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" 
parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

回答by Darush

To hide both status bar and action bar and make your activity full-screen use the following code in your activity's onResume()or onWindowFocusChanged()method:

要隐藏状态栏和操作栏并使您的活动全屏,请在您的活动onResume()onWindowFocusChanged()方法中使用以下代码:

@Override
protected void onResume() {
    super.onResume();
    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN);
}

You can find more Information in the following links:

您可以在以下链接中找到更多信息:

Note:Using the xml solutions provide in this thread I could only hide the status bar but not the navigation bar.

注意:使用此线程中提供的 xml 解决方案,我只能隐藏状态栏而不能隐藏导航栏。

回答by DysaniazzZ

This theme only works after API 21(included). And make both the StatusBar and NavigationBar transparent.

此主题仅在 API 21(包括)之后有效。并使 StatusBar 和 NavigationBar 透明。

<style name="TransparentAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <item name="windowActionBar">false</item>
  <item name="windowNoTitle">true</item>
  <item name="android:statusBarColor">@android:color/transparent</item>
  <item name="android:windowBackground">@android:color/transparent</item>
  <item name="android:navigationBarColor">@android:color/transparent</item>
  <item name="android:windowIsTranslucent">true</item>
  <item name="android:windowContentOverlay">@null</item>
</style>