Android 在全屏模式下隐藏标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/991764/
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 02:33:54  来源:igfitidea点击:

Hiding Title in a Fullscreen mode?

androidfullscreentitle

提问by yanchenko

Is there a way to hide the window title so that it won't get shown in fullscreen mode (

有没有办法隐藏窗口标题,使其不会在全屏模式下显示(

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

) but then will appear upon

) 但随后会出现在

getWindow().clearFlags(LayoutParams.FLAG_FULLSCREEN)

?

?

requestWindowFeature(Window.FEATURE_NO_TITLE)

is not an option of course as this won't allow to get it back.

这当然不是一种选择,因为这不允许将其取回。

回答by snctln

The way I handle this in my Android games is to call the following line in the onCreate() of my Activity

我在 Android 游戏中处理此问题的方式是在我的 Activity 的 onCreate() 中调用以下行

requestWindowFeature(Window.FEATURE_NO_TITLE);

I can then turn the full screen capability off and on using the following code in my activity class (usually called from a menu option) (the m_contentView variable is the view from findViewById() using the id that you used when calling setContentView() in your on create)

然后我可以在我的活动类中使用以下代码关闭和打开全屏功能(通常从菜单选项调用)(m_contentView 变量是 findViewById() 中的视图,使用您在调用 setContentView() 时使用的 id你在创造)

private void updateFullscreenStatus(boolean bUseFullscreen)
{   
   if(bUseFullscreen)
   {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    }
    else
    {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    m_contentView.requestLayout();
}

I use this technique in all of my games without problem.

我在所有游戏中都使用这种技术,没有问题。

Why do you say

你为什么说

requestWindowFeature(Window.FEATURE_NO_TITLE); is not an option of course

requestWindowFeature(Window.FEATURE_NO_TITLE); 当然不是一个选择

?

?

::EDIT::

::编辑::

Well if you are trying to dynamically show and hide it during the lifetime of the activity I am not sure if you can do that with the official Window Title due to the note that has been mentioned about window features needing to be set before setContentView() is called (link)

好吧,如果您试图在活动的生命周期内动态显示和隐藏它,我不确定您是否可以使用官方窗口标题来做到这一点,因为已经提到了关于需要在 setContentView() 之前设置的窗口功能的说明被称为(链接

One thing that you could do is implement your own title bar and dynamically show and hide that... I put together this example that should set you o nthe right track

你可以做的一件事是实现你自己的标题栏并动态显示和隐藏它......我把这个例子放在一起应该让你走上正轨

Here is the layout file

这是布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:fadingEdgeLength="0sp"
    >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myTitleBarLayout" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <TextView
            android:id="@+id/myTitleBarTextView"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:paddingTop="4dip"
            android:paddingBottom="4dip"
            android:paddingLeft="6dip"
            android:textStyle="bold"
            android:shadowColor="#BB000000"
            android:shadowRadius="3.0"
            android:shadowDy=".25"

        />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="#CCEEEEEE"
            android:padding="10dip"
        />
    </LinearLayout>

    <ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="1"
        >

        <!-- Insert your regular layout stuff here -->

        <Button android:id="@+id/toggle_title_button" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="Toggle Title" 
        />
    </ScrollView>
</LinearLayout>

And here is the code for the main activity that will allow you to toggle our custom title bar on and off

这是主要活动的代码,它允许您打开和关闭我们的自定义标题栏

package com.snctln.test.HelloGridView;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HelloGridView extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv = (TextView)this.findViewById(R.id.myTitleBarTextView);
        tv.setBackgroundColor(0xFF848284);
        tv.setTextColor(0xFFFFFFFF);

        Button toggleTitleButton = (Button)this.findViewById(R.id.toggle_title_button);

        toggleTitleButton.setOnClickListener( new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    LinearLayout ll = (LinearLayout)findViewById(R.id.myTitleBarLayout);

                    if(ll.getVisibility() == View.GONE)
                    {
                        ll.setVisibility(View.VISIBLE);
                    }
                    else
                    {
                        ll.setVisibility(View.GONE);
                    }
                }
            });
    }
}

It doesn't look perfect, but you can always play with the layout some more to do that.

它看起来并不完美,但您总是可以使用更多的布局来做到这一点。

alt text

替代文字

My other thought is if you just want to hide everything to show a progress bar why not use the ProgressDialog?

我的另一个想法是,如果您只想隐藏所有内容以显示进度条,为什么不使用ProgressDialog

This class is pretty easy to use...

这个类很容易使用......

progressDlg = ProgressDialog.show(context, getString(R.string.progress_dialog_title_prepare), getString(R.string.progress_dialog_body_prepare));

// do long running operation

if(operationFailed)
{
    progressDlg.cancel();
}
else
{
    progressDlg.dismiss();
}

回答by user1154664

Adding android:theme="@android:style/Theme.NoTitleBar.Fullscreen"to application tag in the manifest file will make every activity fullscreen.

添加android:theme="@android:style/Theme.NoTitleBar.Fullscreen"到清单文件中的应用程序标签将使每个活动全屏显示。

回答by omni.present

To disable the title of your application(it is the app name)

禁用应用程序的标题(它是应用程序名称)

requestWindowFeature(Window.FEATURE_NO_TITLE)

To disable the notification bar on the top(so a request to the android app manager to allow Fullscreen)

禁用顶部的通知栏(因此向 android 应用程序管理器发出请求以允许全屏显示)

getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN)

getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN)

Hope this helps any one who wants to know the difference!!

希望这可以帮助任何想知道其中区别的人!!

回答by David

if(useFullscreen)  
{  
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);  
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);  
}  
else  
{  
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);  
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);  
}  

this worked for me .. at onResume method

这对我有用 .. 在 onResume 方法

回答by Alex Semeniuk

On Android 3+ it can be easilyy achieved by calling getActionBar().hide()and getActionBar().show()to respectively show and hide the standard ActionBar

在 Android 3+ 上,可以通过调用getActionBar().hide()getActionBar().show()分别显示和隐藏标准 ActionBar来轻松实现

On Android 1,2 the best solution (I guess) is to implement custom View for yout "title bar" and hide it on demand (of course, calling to requestWindowFeature(Window.FEATURE_NO_TITLE);at the beginning).

在 Android 1,2 上,最好的解决方案(我猜)是为你的“标题栏”实现自定义视图并按需隐藏它(当然,requestWindowFeature(Window.FEATURE_NO_TITLE);在开始时调用)。

回答by reflog

that is not possible according to the docs and the android-developers google group. to implement that , you need to add a 'title-bar-like' layout item with your text and progress bar and hide/show when you need it. right now - no other way around it, since title bar control can be done only before the setContentView call and not changed after.

根据文档和 android-developers google group,这是不可能的。要实现这一点,您需要添加一个带有文本和进度条的“类似标题栏”的布局项,并在需要时隐藏/显示。现在——别无他法,因为标题栏控制只能在 setContentView 调用之前完成,之后不能更改。