Android--切换屏幕方向

main.xml代码:

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

    <Button
        android:id="@+id/mybtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改变屏幕方向(当前为竖屏显示)" />

    <ImageView
        android:id="@+id/myview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/aa"/>

</LinearLayout>

.java代码如下:

package org.lxh.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class Hello extends Activity {
	private Button change = null;
	private ImageView img = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); // 生命周期方法
		super.setContentView(R.layout.main); // 设置要使用的布局管理器

		this.change = (Button) super.findViewById(R.id.mybtn);
		this.img = (ImageView) super.findViewById(R.id.myview);
		this.change.setOnClickListener(new OnClickListenerImpl());

	}

	private class OnClickListenerImpl implements OnClickListener {

		public void onClick(View arg0) {
			if (Hello.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
				Hello.this.change.setText("无法进行屏幕方向");
			} else {
				if (Hello.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
					Hello.this
							.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
				} else if (Hello.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
					Hello.this
							.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
				}
			}

		}

	}
}

需要配置权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest
	xmlns:android="http://schemas.android.com/apk/res/android"
	package="org.lxh.demo"
	android:versionCode="1"
	android:versionName="1.0">
	<uses-sdk android:minSdkVersion="10" />
<strong>	<span style="color:#ff0000;"><uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/></span></strong>
	<application
		android:icon="@drawable/icon"
		android:label="@string/app_name">
		<activity
			android:name=".Hello"
			android:label="@string/app_name"
			<strong><span style="color:#ff0000;">android:configChanges="orientation|keyboard"
		     android:screenOrientation="portrait"</span></strong>>
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
	</application>
</manifest>

时间: 2024-08-30 02:59:43

Android--切换屏幕方向的相关文章

Android自适应屏幕方向、大小和分辨率,及字体设置

屏幕大小 1.不同的layout Android手机屏幕大小不一,有480x320,640x360,800x480.怎样才能让App自动适应不同的屏幕呢? 其实很简单,只需要在 res目录下创建不同的layout文件夹,比如:layout-640x360,layout-800x480,所有的layout文件在编译之后都会 写入R.java里,而系统会根据屏幕的大小自己选择合适的layout进行使用. 2.hdpi.mdpi.ldpi 之前的版本中,只有一个drawable,而2.1版本中有dra

Android screenOrientation 屏幕方向的设定与控制

AndroidManifest.xml中Activity标签中的设定值: android:screenOrientation="landscape"为90度(横屏) android:screenOrientation="reverseLandscape"为270度(反向横屏) android:screenOrientation="sensorLandscape"为90度和270度根据G-sensor切换(横屏切换) android:screenO

android自适应屏幕方向和大小

一:不同的layout Android手机屏幕大小不一,有480x320, 640x360, 800x480.怎样才能让App自动适应不同的屏幕呢?    其实很简单,只需要在res目录下创建不同的layout文件夹,比如layout-640x360,layout-800x480,所有的layout文件在编译之后都会写入R.java里,而系统会根据屏幕的大小自己选择合适的layout进行使用. 二:hdpi.mdpi.ldpi 在之前的版本中,只有一个drawable,而2.1版本中有drawa

android之屏幕方向切换

今天看到andoird屏幕的切换,因为在游戏中切换横向时非常正常的一件事. 首先如果我们不想切换方向那么我们可以在androidmainfest.xml中对activity的属性进行添加. android:sreenOrientation 属性值 portrait表示垂直 landscape表示水平 但是我不想这么做,我添加水平和垂直按钮实现屏幕的切换. 添加两个按钮,在监听函数中实现 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION

Android设置屏幕方向

设置方法:在AndroidManifest.xml中的Activity里加一个属性android:screenOrientation.例如设置该Activity为横向 <activity android:name=".Login"android:screenOrientation="landscape"></activity> screenOrientation的值有以下几种:landscape:横向portrait:纵向unspecifie

Android Studio 屏幕方向以及UI界面状态的保存

package com.example.orientation; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity ext

android切换屏幕时的生命周期

老版本总结: 1.不设置Activity的android:configChanges时 切屏会重新调用生命周期的方法,切横屏调用1次 切竖屏调用2次 2.设置Activity的android:configChanges ="orientation"时,切横屏和竖屏都是调用1次生命周期 3.设置Activity的android:configChanges = "orientation|KeyboardHidden"时切屏不会调用生命周期的方法,只会调用onConfig

Android中Activity运行时屏幕方向与显示方式详解

现在我们的手机一般都内置有方向感应器,手机屏幕会根据所处位置自动进行横竖屏切换(前提是未锁定屏幕方向).但有时我们的应用程序仅限在横屏或者竖屏状态下才可以运行,此时我们需要锁定该程序Activity运行时的屏幕方向.还有就是在我们用手机观看视频时,随意的进行横竖屏切换,但播放进度不会随着屏幕的转换而从头开始播放,为了实现这个功能,我们就需要在Activity转换时对当前数据进行保存. 现在根据以上两种需求,个人提出以下解决方案: 一.锁定Activity运行时屏幕方向,如下图(演示锁定横屏):

如果写一个android桌面滑动切换屏幕的控件(三)

下面我们把这个控件内嵌到Layout中做一些动画和展示,效果图: 这个子控件可以上下移动,可以左右滑动,如果上下滑动距离大于左右滑动距离,则必须上下滑动 这样来写onTouch事件: @Override public boolean onTouchEvent(MotionEvent ev) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMov

详解Android中的屏幕方向

详解Android中的屏幕方向 屏幕方向 是对Activity而言的,所以你可以在AndroidManifest.xml 文件中,通过<activity> 标记的screenOrientation 属性进行设定,例如: <activity android:name=".SketchpadActivity" android:screenOrientation="landscape"/><!--让该Activity总是显示为横屏-->