3种Android隐藏顶部状态栏及标题栏的方法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
3种Android隐藏顶部状态栏及标题栏的⽅法
本⽂包含3种隐藏顶部状态栏及标题栏和⼀种隐藏Android 4.0平板底部状态栏的⽅法,分享给⼤家供⼤家参考,具体内容如下⽅法⼀
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 隐藏标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 隐藏状态栏
getWindow().setFlags(youtParams.FLAG_FULLSCREEN,
youtParams.FLAG_FULLSCREEN);
setContentView(yout.activity_main);
}
}
⽅法⼆
<!-- 同时隐藏状态栏和标题栏 -->
<activity
android:name="com.ysj.demo.MainActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="UNCHER" />
</intent-filter>
</activity>
⽅法三
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<!-- 隐藏状态栏 -->
<item name="android:windowFullscreen">true</item>
<!-- 隐藏标题栏 -->
<item name="android:windowNoTitle">true</item>
</style>
注:
1、⽅法⼀中的两段代码要在setContentView()之前。
2、⽅法⼆只能同时隐藏状态栏和标题栏。
3、⽅法⼀和⽅法⼆都只应⽤于单个Activity。
⽅法三应⽤于整个程序。
对于运⾏Android 4.0以上系统的平板电脑,以上三种⽅法都不会隐藏屏幕下⽅的状态栏,须做如下处理。
public class StartupActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(yout.activity_startup);
/*
* 隐藏运⾏Android 4.0以上系统的平板的屏幕下⽅的状态栏
*/
try
{
String ProcID = "79";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) ProcID = "42"; // ICS
// 需要root 权限
Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "service call activity " + ProcID + " s16 com.android.systemui" }); // WAS proc.waitFor();
}
catch (Exception ex)
{
Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
protected void onDestroy()
{
// TODO Auto-generated method stub
/*
* 恢复运⾏Android 4.0以上系统的平板的屏幕下⽅的状态栏
*/
try
{
Process proc = Runtime.getRuntime().exec(new String[] { "am", "startservice", "-n", "com.android.systemui/.SystemUIService" });
proc.waitFor();
}
catch (Exception e)
{
e.printStackTrace();
}
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.startup, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO Auto-generated method stub
switch (item.getItemId())
{
case R.id.action_exit:
finish();
break;
}
return true;
}
}
由于没有了状态栏,须在程序中提供退出程序的⽅法。
希望本⽂所述对⼤家学习Android软件编程有所帮助。