我在 Android 游戏中处理此问题的方法是在 Activity 的 onCreate() 中调用以下行
requestWindowFeature(Window.FEATURE_NO_TITLE);
然后,我可以在我的活动类中使用以下代码(通常从菜单选项调用)关闭和打开全屏功能(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();
}
我在所有游戏中都使用了这种技术,没有出现任何问题。
你为什么说
requestWindowFeature(Window.FEATURE_NO_TITLE);
当然不是一个选择
?
::EDIT::
好吧,如果您尝试在活动的生命周期内动态显示和隐藏它,我不确定您是否可以使用官方窗口标题来做到这一点,因为已经提到了有关需要在 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>
这是主要活动的代码,它允许您打开和关闭我们的自定义标题栏
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);
}
}
});
}
}
它看起来并不完美,但您始终可以对布局进行更多尝试来做到这一点。
我的另一个想法是,如果您只是想隐藏所有内容以显示进度条,为什么不使用进度对话框?
这个类非常容易使用......
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();
}