❶ 如何设置android界面横屏显示
全屏
在Activity的onCreate方法中的setContentView(myview)调用之前添加下面代码
requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
横屏
按照下面代码示例修改Activity的onResume方法
@Override
protected void onResume() {
/**
* 设置为横屏
*/
if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
super.onResume();
}
或者在配置文件中对Activity节点添加android:screenOrientation属性(landscape是横向,portrait是纵向)
android:launchMode="singleTask" android:screenOrientation="portrait">
要设置成竖屏设置成 SCREEN_ORIENTATION_PORTRAIT
// ----------------
常亮
view.setKeepScreenOn(true)
不加任何旋转屏幕的处理代码的时候,旋转屏幕将会导致系统把当前activity关闭,重新打开。
如果只是简单的界面调整,我们可以阻止此问题的发生,屏幕旋转而自己调整屏幕的元素重构。
首先我们需要修改AndroidManifest.xml文件:
<activity android:name=".Magazine">
</activity>
//修改为:
<activity android:name=".Magazine"
android:configChanges="orientation|keyboard">
</activity>
这样是让程序能够响应旋转屏幕的事件。
然后重写onConfigurationChanged方法:
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.v(" == onConfigurationChanged");
processLayout();
}
//----------------------------
在我们用Android开发过程中,会碰到Activity在切换到后台或布局从横屏LANDSCAPE切换到PORTRAIT,会重新切换Activity会触发一次onCreate方法。
在Android开发中这种情况视可以避免的,我们可以在androidmanifest.xml中的activit元素加入这个属性 android:configChanges="orientation|keyboardHidden" 就能有效避免oncreat方法的重复加载,
androidmanifest.xml内容如下:红色字体为添加部分
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".DemoGPS"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps" />
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
</manifest>
同时在Activity的java文件中重载onConfigurationChanged(Configuration newConfig)这个方法,这样就不会在布局切换或窗口切换时重载等方法。代码如下:
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//land
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
//port
}
}
//------------------------------------------------------
关于Android中Activity的横竖屏切换问题可以通过AndroidManifest.xml文件中的Activity来配置:
android:screenOrientation=["unspecified" | "user" | "behind" |
"landscape" | "portrait" |
"sensor" | "nonsensor"]
screenOrientation 用来指定Activity的在设备上显示的方向,每个值代表如下含义:
"unspecified" 默认值 由系统来判断显示方向.判定的策略是和设备相关的,所以不同的设备会有不同的显示方向.
"landscape" 横屏显示(宽比高要长)
"portrait" 竖屏显示(高比宽要长)
"user" 用户当前首选的方向
"behind" 和该Activity下面的那个Activity的方向一致(在Activity堆栈中的)
"sensor" 有物理的感应器来决定。如果用户旋转设备这屏幕会横竖屏切换。
"nosensor" 忽略物理感应器,这样就不会随着用户旋转设备而更改了 ( "unspecified"设置除外 )。
更多安卓例子请去360手机助手下载安卓学习手册,里面有横竖排例子,源码,例子随便看。
❷ 安卓系统开发 怎么强制系统横屏
两种方法,一种是AndroidManifest.xml里activity加上
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
第一个是屏幕方向横屏,第二个是避免横屏activity被重新创建(onCreate会执行两次)
还有一种办法就是代码切换横屏
activity的方法setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
❸ android开发手机横屏会有什么影响
横屏的话,Activity的生命周期会重新调用一遍,相当于app重新启动,这时候伊一定要注意数据的保存和读取,防止丢失用户的数据。
❹ 安卓手机如何开启横屏模式
1.先找到并打开设置
拓展资料
Android 设置横屏模式显示,横屏模式大部分情况能弥补竖屏的不足——字体小、键盘小、画幅比例不合适,而且横屏模式下能提供更华丽、更流畅的感官体验,android 横屏,android landscape mode ,android 强制横屏,android 强制竖屏,android 横屏布局,android 设置全屏模式。
❺ android开发横竖屏问题
Android横屏竖屏设置
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置成全屏模式
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE););//强制为横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
我做的东西里面还用到了去掉标题栏。
我也贴出来
requestWindowFeature(Window.FEATURE_NO_TITLE);
垂直居中:
android:layout_centerVertical="true"
水平居中:
android:layout_centerHorizontal="true"
1.hideStatusbarAndTitlebar()隐藏statusbar和titlebar.
private void hideStatusbarAndTitlebar() {
final Window win = getWindow();
// No Statusbar
win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// No Titlebar
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
2.设置屏幕显示模式ScreenOrientation.
在activity里设置android:screenOrientation的值。
android:screenOrientation的属性有以下值:
unspecified(默 认值,由系统判断状态自动切换),The default value. The system chooses the orientation. The policy it uses, and therefore the choices made in specific contexts, may differ from device to device.
landscape,横屏
portrait,竖屏
user(用户当前设置的orientation值),The user's current preferred orientation.
behind(下一个要显示的Activity的orientation值),The same orientation as the activity that's immediately beneath it in the activity stack.
sensor(传 感器的方向),The orientation determined by a physical orientation sensor. The orientation of the display depends on how the user is holding the device; it changes when the user rotates the device.
nosensor(不 使用传感器,这个效果差不多等于unspecified).An orientation determined without reference to a physical orientation sensor. The sensor is ignored, so the display will not rotate based on how the user moves the device. Except for this distinction, the system chooses the orientation using the same policy as for the "unspecified" setting.
3.水平/垂直居中的方法.
设置parent的android:gravity为"center"。
4.获得当前屏幕宽高的方法.
Display display = getWindowManager().getDefaultDisplay();
Config.screenWidth = display.getWidth();
Config.screenHeight = display.getHeight();
❻ GPHONE:android如何实现横屏
当屏幕变为横屏的时候,系统会重新呼叫当前Activity的OnCreate方法,你可以把以下方法放在你的OnCreate中来检查当前的方向,然后可以让你的SetContentView来载入不同的Layout xml.
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.i("info", "landscape");
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.i("info", "portrait");
}
关于屏幕切换的时候
首先需要在androidmanifest.xml中加入配置
android:configChanges="orientation|keyboardHidden|navigation
这样在程序中. Activity就不会重复的调用onCreate()
甚至不会调用onPause.onResume.
只会调用一个onConfigurationChanged(Configuration newConfig)
这是在XML加入配置选项的前提下.
如果在就加入选项的前提下.如上所说. Activity会重新激活onCreate方法
根据你自己的需求来选择配置改变时的处理机制这样比较好一点。
❼ Android开发中如何强制横屏和强制竖屏设置
在Androidmanifest.xml里面设置,
android:screenOrientation="portrait" 竖屏
android:screenOrientation="landscape" 横屏
❽ 安卓开发怎样设置成横屏,我做了个游戏,一直是竖屏的,不方便
在.xml文件中可以设置默认方向的,AndroidManifest.xml中设置activity中的android:screenOrientation属性值
❾ Android 漫画APP 竖屏 横屏切换技术实现问题
、禁止横竖屏转换
Android横竖屏切换在手机开发中比较常见,很多软件在开发过程中为了避免横竖屏切换时引发不必要的麻烦,通常禁止掉横竖屏的切换,
通过在AndroidManifest.xml中设置activity中的android:screenOrientation属性值来实现。
比如下列设置
android:screenOrientation="portrait"
则无论手机如何变动,拥有这个属性的activity都将是竖屏显示。
android:screenOrientation="landscape",为横屏显示。
上述修改也可以在Java代码中通过类似如下代码来设置
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
另外,android中每次屏幕的切换动会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次Create的时候载入配置,那样,进行中的游戏就不会自动重启了!
二、横竖屏切换
如果要让软件在横竖屏之间切换,由于横竖屏的高宽会发生转换,有可能会要求不同的布局。可以通过以下两种方法来切换布局:
1)在res目录下建立layout-land和layout-port目录,相应的layout文件名不变,比如main.xml。layout-land是横屏的layout,layout-port是竖屏的layout,其他的不用管,横竖屏切换时程序为调用Activity的onCreate方法,从而加载相应的布局。
2)假如布局资源不按照如上设置,则可以通过java代码来判断当前是横屏还是竖屏然后来加载相应的xml布局文件。因为当屏幕变为横屏的时候,系统会重新呼叫当前Activity的onCreate方法,你可以把以下方法放在你的onCreate中来检查当前的方向,然后可以让你的setContentView来载入不同的layout xml。
if(this.getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE)
{
Log.i("info", "landscape"); // 横屏
} else if(this.getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT)
{
Log.i("info", "portrait"); // 竖屏
}
三、通过onConfigurationChanged拦截横竖屏变换
按照二的操作,Activity每次横竖屏切换都会重新调用onPause-> onStop-> onDestory-> onCreate->onStart->onResume,为此涉及到内容和数据的保存和读取,否则转屏之前的内容就会消失了。很多时候这样的结果让程序繁琐,为此Android提供了在manifest中设置android:configChanges属性,从而让Activity不延续上述的重建流程。在Android工程的Mainfest.xml中配置Activity:android:configChanges="keyboardHidden|orientation",横竖屏切换之后就不会去执行OnCreat函数了,而是会去调用onConfigurationChanged()这样就能控制横竖屏的切换了。用户可以在Activity或View的onConfigurationChanged(Configuration newConfig)函数中获取当前横竖屏参数。至于其调用顺序跟touch时间的传递顺序相似,不过他没有消费事件的概念,会顺次调用到每一个onConfigurationChanged函数。
需要重写Activity的onConfigurationChanged方法。实现方式如下,不需要做太多的内容:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// land do nothing is ok
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// port do nothing is ok
}
}
需要注意的是,onConfigurationChanged函数中只能获得横竖屏切换后的参数,在该函数中获取不到新的Layout和控件的尺寸位置信息,如果要处理尺寸和位置信息,必须通过消息异步或者延时调用。
四、彻底禁止翻转
当然如果要彻底禁止翻转,可以设置android:screenOrientation的属性为nosensor,如此就可以忽略重力感应带来的麻烦了。不过在模拟器上不管用,在真机上是正确的。
这里提一个小知识,Android模拟器中,快捷键"Ctrl+F11/F12"可以实现转屏
五,自适应转换
如果想让它启动的时候是横屏的话就横屏表示,纵屏的话就纵屏表示,然后手机切换横竖屏就不能用了该怎么解决呢?
首先:在Mainfest.xml中追加
android:screenOrientation="sensor" android:configChanges="orientation|keyboardHidden"
这两个属性。
❿ android开发怎么横屏切换效果
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
Log.i("info","landscape"); // 横屏
}
else if(this.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
Log.i("info","portrait"); // 竖屏