`
007007jing
  • 浏览: 41266 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

android2.3 api demo 学习系列(14)--App/Activity/Screen Orientation

阅读更多

下面我们来学习下Screen Orientaiton的demo。

1、首先我们在values下面新建文件arrays.xml(用来在下拉列表中显示)

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <!-- Used in app/Screen Orientation -->
    <string-array name="screen_orientations"
        <item>UNSPECIFIED</item>
        <item>LANDSCAPE</item>
        <item>PORTRAIT</item>
        <item>USER</item>
        <item>BEHIND</item>
        <item>SENSOR</item>
        <item>NOSENSOR</item>
        <item>SENSOR_LANDSCAPE</item>
        <item>SENSOR_PORTRAIT</item>
        <item>REVERSE_LANDSCAPE</item>
        <item>REVERSE_PORTRAIT</item>
        <item>FULL_SENSOR</item>
    </string-array>
</resources>

 

 2、我们在代码中定义相对应数组

 

 final static int mOrientationValues[] = new int[] {
        ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED,
        ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
        ActivityInfo.SCREEN_ORIENTATION_USER,
        ActivityInfo.SCREEN_ORIENTATION_BEHIND,
        ActivityInfo.SCREEN_ORIENTATION_SENSOR,
        ActivityInfo.SCREEN_ORIENTATION_NOSENSOR,
        ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE,
        ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT,
        ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE,
        ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
        ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR,
    };

 

 3、处理用户选择

 

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_activity_screen_orientation);
        
        mOrientation = (Spinner)findViewById(R.id.app_activity_screen_orientation);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.screen_orientations, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mOrientation.setAdapter(adapter);
        mOrientation.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {
                        setRequestedOrientation(mOrientationValues[position]);
                    }

                    public void onNothingSelected(AdapterView<?> parent) {
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
                    }
                });
    }

 效果图:



 

相关方法:

createFromResource()方法创建一个新的 ArrayAdapter, 定义了数组中得每一项如何显示在下来列表上.  setDropDownViewResource(int)定义了下拉列表展开时每一项如何显示.

AdapterView.OnItemSelectedListener 要求必须有 onItemSelected() 和onNothingSelected() 两个回调方法.根据其方面名我们就可以了解其用途。

 

 

public void setRequestedOrientation (int requestedOrientation)

改变activity的方向. 如果activity正在显示,则改变会立刻生效 (有可能导致activity重起). 否则改变会在activity下次可见的时候生效。

参数requestedOrientation可选值:

SCREEN_ORIENTATION_UNSPECIFIED,SCREEN_ORIENTATION_LANDSCAPESCREEN_ORIENTATION_PORTRAIT

SCREEN_ORIENTATION_USERSCREEN_ORIENTATION_BEHINDSCREEN_ORIENTATION_SENSOR,

SCREEN_ORIENTATION_NOSENSORSCREEN_ORIENTATION_SENSOR_LANDSCAPE

SCREEN_ORIENTATION_SENSOR_PORTRAITSCREEN_ORIENTATION_REVERSE_LANDSCAPE,

SCREEN_ORIENTATION_REVERSE_PORTRAITSCREEN_ORIENTATION_FULL_SENSOR.


.

 

 

  • 大小: 34.1 KB
  • 大小: 33.9 KB
分享到:
评论

相关推荐

    Android代码-dlib-android-app

    dlib-android-app See http://dlib.net for the main project documentation. See dlib-android for JNI lib. Refer to dlib-android/jni/jnilib_ex Grap the source $ git clone ...

    安卓串口demo(android-serialport-api-android-serialport-api

    安卓串口Demo程序 基于谷歌提供的源码android-serialport-api-android-serialport-api

    React-Native官方demo(对应 最新v0.55.0版本20180620编译)

    1.D:\reactnative\RNTester&gt; react-native bundle --platform android --dev false --entry-file js/RNTesterApp.android.js --bundle-output android/app/src/main/assets/RNTesterApp.android.bundle --assets-...

    Android代码-cordova-plugin-app-update

    cordova-plugin-app-update App updater for Cordova/PhoneGap Demo Try it yourself: Just clone and install this demo. cordova-plugin-app-update-DEMO :tada: 如果喜欢它,请别忘了给我一颗鼓励的星 Support ...

    android-serialport-api 串口demo.zip

    这是很据android-serialport-api 自己简化的一个demo ,可以使用。原来android-serial-api的程序很多人反映都不能使用,所以自己写了这个,只有一个activity,可以做为你的学习参考。

    WebRTCDemo-master_android-studio

    WebRTCDemo-master_android-studio,webrtcdemo,p2p,android-studio编译

    upload-download

    准备工作$ npm install启动服务找到对应的实例代码,然后启动 所在目录的 server/app.js如node ./src/upfiles-demo/demo-a1-form/server/app.jsnode ./src/upfiles-demo/demo-a2-formdata/server/app.jsnode ./src/...

    基于mAppwidget实现的手绘地图demo

    可使用不同的图片作为地图...//下载demo http://mapp.android-libraries.com/download //学习地图 http://www.67tgb.com/?p=610 图片切割 http://www.67tgb.com/?p=597 http://mapp.android-libraries.com/slicingtool/

    Android语音识别示例

    由于最近在做智能家居方向的产品,需要在App上对机器人实现一个简单的语音控制,于是开始寻找相应的解决方案,由于某种原因,google自己的语音识别API并不能在国内发挥作用,所以我们选择国内的科大讯飞语音识别服务...

    android 项目 开发 Demo

    android 项目 开发 demo &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demo" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:...

    Android代码-安卓串口打开、接收与发送

    Android串口操作,简化android-serialport-api的demo(附源码) 我把文中的源码导入 android studio 源码来自谷歌:android-serialport-api 涉及到的 jni 开发可以参考:【Android 应用开发】Android 开发 之 JNI...

    SAML协议交互,实现工程Demo(有注释)

    OpenSAML-ref-project-demo-v3 这一个使用OpenSAML库的示例项目: 一个很简单的网址,其充当SP;同时该项目还包括一个很简单的IDP; SAML协议的交互将在这二者之间展开。项目启动之后,访问如下网址: ...

    Face-api.js静态页面版Demo

    JavaScript人脸识别库Face-api.js的示例,无需安装nodejs,iis本地直接看效果。注意调用摄像头不能用IP访问,只能localhost,远程预览需要HTTPS;iis无扩展名文件若出现404,需在mime类型中添加扩展名【.】类型...

    ci-demo-app-homebrew-tap:CI演示应用程序的自制方法

    ci-demo-app-homebrew-tap CI演示应用程序的自制方法 安装 注册水龙头: brew tap embano1/ci-demo-app git@github.com:embano1/ci-demo-app-homebrew-tap 注意:如果您没有使用ssh设置git,您也可以使用...

    Android SipDemo( 网络电话的例子源码,android 2.3以上才能用)

    这是android 2.3及2.3以后才能用的Sip (网络电话)相关接口使用的例子,实现了基本的网络电话功能(呼叫及被呼叫),用的是Android提供的编程接口。

    Android代码-DroidDrop–Remote-Logging

    DroidDrop - Remote Logging DroidDrop Remote logging give Android Developers the ability to log data from their applications to a drop on drop.io. ...Get a developer API Key from drop.io (free

    android demo api

    android demo 的例子 用来学习android的效果 ,view 都是很好的帮助 还是中文的 通俗易懂 谢谢

    Android服务端API仿真 - SpringBoot入门 - demo

    文章地址 http://blog.csdn.net/ccslff/article/details/56675644

    android-demo-app:PyTorch android在应用程序中的用法示例

    是一个简单的图像分类应用程序,演示了如何使用PyTorch Android API。 PyTorch演示应用 是一款功能完善的应用程序,其中包含两个展示柜。 相机应用程序运行量化模型以实时对图像进行分类。 还有一个基于文本的应用...

    Android 多种android控件的Demo-IT计算机-毕业设计.zip

    Android应用源码开发Demo,主要用于毕业设计学习。

Global site tag (gtag.js) - Google Analytics