android - 在全屏模式下隐藏标题?

标签 android fullscreen title

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

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

) 但随后会出现在

getWindow().clearFlags(LayoutParams.FLAG_FULLSCREEN)

?

requestWindowFeature(Window.FEATURE_NO_TITLE)

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

最佳答案

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

requestWindowFeature(Window.FEATURE_NO_TITLE);

然后我可以在我的 Activity 类(通常从菜单选项中调用)中使用以下代码关闭和打开全屏功能(m_contentView 变量是使用调用 setContentView 时使用的 id 来自 findViewById() 的 View () 在您的创建中)

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();
}

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

为什么说

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

?

::编辑::

好吧,如果您尝试在 Activity 的生命周期内动态显示和隐藏它,我不确定您是否可以使用官方窗口标题来做到这一点,因为之前提到过需要设置窗口功能的注释调用 setContentView() (link)

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

这是布局文件

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

这是主要 Activity 的代码,可让您打开和关闭我们的自定义标题栏

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);
                    }
                }
            });
    }
}

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

alt text

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

这个类很容易使用...

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();
}

关于android - 在全屏模式下隐藏标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/991764/

相关文章:

python - 如何在 basemap 图形的标题下方添加间距?

android - 如何在 Android 中显示带有百分比的 ProgressBar 示例

android - 如何去除顶部状态栏黑色背景

objective-c - OSX - 从缩放按钮禁用全屏模式?

ios - 如何手动设置 titleForHeaderInSection

extjs - 如何更改网格标题的字体大小 - ExtJS?

android - 布局设计问题

android - 我在哪里可以将我的自定义模板放在 Android Studio 4.1 中

java - 将 9-patch drawable 应用到 Button,保持默认填充

html - 全屏响应式视频播放器