android横屏竖屏处理
16lz
2021-01-23
android里如何让改变屏幕的方向,今天我就谈谈这个问题。
首先我们得知道android里常见两个单词的意思:
PORTRAIT 横屏
LANDSCAPE 竖屏
好了,来个例子相信跟直观。
Step 1:我们建立一个Android工程,命名为ChangeOrientationDemo.
Step 2:设计UI,打开main.xml,将其代码修改如下,我们这里只是增加了一个按钮,其他什么都没有动.<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press me Change Orientation"
/>
</LinearLayout>
Step 3:设计主程序ChangeOrientationDemo.java,修改其代码如下:
package com.android.test;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ChangeOrientationDemo extends Activity {
private Button bt1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取资源
bt1 = (Button)findViewById(R.id.bt1);
//增加按钮事件
bt1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
//如果是竖排,则改为横排
if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
//如果是横排,则改为竖排
else if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
});
}
}
Step 4:在AndroidManifest.xml文件里设置默认方向,不然程序不能正常工作哦.代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ChangeOrientationDemo"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
好了!如果当前屏幕为横屏,点击一下按钮则变为竖屏,若当前屏幕为竖屏,点击一下按钮变为横屏。
参考博客:http://weizhulin.blog.51cto.com/1556324/311469/
首先我们得知道android里常见两个单词的意思:
PORTRAIT 横屏
LANDSCAPE 竖屏
好了,来个例子相信跟直观。
Step 1:我们建立一个Android工程,命名为ChangeOrientationDemo.
Step 2:设计UI,打开main.xml,将其代码修改如下,我们这里只是增加了一个按钮,其他什么都没有动.<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press me Change Orientation"
/>
</LinearLayout>
Step 3:设计主程序ChangeOrientationDemo.java,修改其代码如下:
package com.android.test;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ChangeOrientationDemo extends Activity {
private Button bt1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取资源
bt1 = (Button)findViewById(R.id.bt1);
//增加按钮事件
bt1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
//如果是竖排,则改为横排
if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
//如果是横排,则改为竖排
else if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
});
}
}
Step 4:在AndroidManifest.xml文件里设置默认方向,不然程序不能正常工作哦.代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ChangeOrientationDemo"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
好了!如果当前屏幕为横屏,点击一下按钮则变为竖屏,若当前屏幕为竖屏,点击一下按钮变为横屏。
参考博客:http://weizhulin.blog.51cto.com/1556324/311469/
更多相关文章
- Android处理屏幕旋转时的解决方案
- Android手机屏幕的三种状态
- android多屏幕适配(1)
- Android屏幕解锁图案破解
- android android屏幕禁止休眠和锁屏的方法
- android背景选择器selector用法、自定义Button按钮样式
- Android中的Shape使用总结—圆角的按钮
- Android的Activity屏幕切换动画||GestureDetector、OnGestureLis