有关phonegap 应用在android平台全屏
问题: 利用phonegap 打包android 程序 在设备上全屏显示;
我们有时需要把 Activity 设置成全屏显示,一般情况下,可以通过两种方式来设置全屏显示效果。
一,通过在代码中可以设置,
代码中设置(如下)
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置无标题 requestWindowFeature(Window.FEATURE_NO_TITLE); //设置全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); }
但要注意的是:在代码中设置的话,设置无标题和设置全屏的两段代码要放置在 setContentView(R.layout.main); 这段代码的前面。要不然会报错。
其二:在manifest配置文件中设置
代码如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.andyidea" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon"
android:label="@string/app_name" android:theme="@android:style/android.NoTitleBar.Fullscreen"> <activity android:name=".login.LoginActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
这个样设置解决不了问题。发现用phonegap打包出来任然不是全屏
在方法一种添加一行代码
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);// 清除FLAG 这行是重点 requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
完整的代码:
phonegap 打包完成代码
public class SaleActivity extends DroidGap { private static final String TARGET_URL = "http:"; private Context context = this; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setFullscreen(); if (checkNetwork()) { super.init(); super.appView.clearCache(true); super.setBooleanProperty("loadInWebView", true); super.setIntegerProperty("loadUrlTimeoutValue", 10000); // 加载时间 super.setIntegerProperty("splashscreen", R.drawable.splash);// 引导页 super.loadUrl(TARGET_URL, 8000); // 设置页面 super.setBooleanProperty("keepRunning", true); // 保持在后台运行 } else { showDialog(); } } // 检查网络状态 private boolean checkNetwork() { ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected() && networkInfo.isAvailable()) { return true; } else { return false; } } // 弹出对话框 protected void showDialog() { AlertDialog.Builder builder = new Builder(context); builder.setMessage("当前未连接网络,是否设置?"); builder.setTitle("提示"); builder.setPositiveButton("确认", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { SaleActivity.this.finish(); Intent intent = new Intent("android.settings.WIRELESS_SETTINGS"); startActivity(intent); } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { SaleActivity.this.finish(); } }); builder.create().show(); } // 全屏(无标题栏和状态栏) protected void setFullscreen() { getWindow().clearFlags( WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);// 清除FLAG requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } }