Android设备onConfigurationChanged事件不处理方向 [英] Android Device onConfigurationChanged event does not handling orientation

查看:291
本文介绍了Android设备onConfigurationChanged事件不处理方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活性和活性的启动我需要稍后我想作为用户旋转装置同时处理orientaion变化改变lanscape的方向,然后,但它一旦改变了方向,后来不改变方向。这里是我的code,请帮助

 公共类HLS延伸活动{@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_hls);    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}@覆盖
公共无效onConfigurationChanged(配置NEWCONFIG){
    super.onConfigurationChanged(NEWCONFIG);    Log.v(新的方向,是);    如果(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        Toast.makeText(这一点,肖像,Toast.LENGTH_LONG).show();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    如果(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        Toast.makeText(这一点,风景,Toast.LENGTH_LONG).show();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }}


解决方案

在使用的onCreate setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)将永久修复你的方向为横向,并避免方向的任何变化。这就是为什么你的方向被固定,不向旋转回应。

下面是您可以使用一种替代方案: -

1>创建布局两个视图。即一个景观,一个用于人像views.Lets说activity_hls_land和activity_hls_port。

2>使用的setContentView(R.layout.activity_hls)在onConfigurationChanged(配置NEWCONFIG),而不是setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)或setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

下面是示例code: -

 公共类MainActivity延伸活动{
布尔isLaunched = TRUE;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        如果(isLaunched){
        Toast.makeText(这一点,1日推出,Toast.LENGTH_SHORT).show();
        的setContentView(R.layout.activity_hls_land);
        }    }    @覆盖
    公共无效onConfigurationChanged(配置NEWCONFIG){
        super.onConfigurationChanged(NEWCONFIG);    如果(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        Toast.makeText(这一点,肖像,Toast.LENGTH_SHORT).show();
        的setContentView(R.layout.activity_hls_port);
    }
     如果(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        Toast.makeText(这一点,风景,Toast.LENGTH_SHORT).show();
        的setContentView(R.layout.activity_hls_land);
    }
    }}

在清单中添加的android:在活动configChanges =方向: -

 <活动
            机器人:名字=。MainActivity
            机器人:标签=@字符串/ title_activity_main
            机器人:configChanges =方向
            >            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
          < /活性GT;

样布局activity_hls_port: -

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/参考hello world
        工具:上下文=。MainActivity/>
 <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/参考hello world
        工具:上下文=。MainActivity/>
  <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/参考hello world
        工具:上下文=。MainActivity/>
< / LinearLayout中>

样品横向模式: -

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直
    机器人:旋转=90>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/参考hello world
        工具:上下文=。MainActivity/>
 <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/参考hello world
        工具:上下文=。MainActivity/>
  <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/参考hello world
        工具:上下文=。MainActivity/>
< / LinearLayout中>

i have an activity and on startup of the activity i need to change the orientation in lanscape and then later on i want to handle both orientaion changes as user rotates device , but it once changes the orientation and later on does not change the orientation. here is my code please help

    public class Hls extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hls);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


}



@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    Log.v("new orientation", "yes");

    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        Toast.makeText(this, "portrait", Toast.LENGTH_LONG).show();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        Toast.makeText(this, "landscape", Toast.LENGTH_LONG).show();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    } 

}

解决方案

Using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) in onCreate will fix your orientation permanently to landscape and will avoid any change in orientation. That is why your orientation gets fixed and doesn't respond to the rotations.

Here is an alternative you can use :-

1> Create two views in your layout. i.e one for landscape and one for portrait views.Lets say activity_hls_land and activity_hls_port.

2> Use setContentView(R.layout.activity_hls) in onConfigurationChanged(Configuration newConfig) instead of setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) or setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

Here is the sample code:-

 public class MainActivity extends Activity {
boolean isLaunched=true;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if(isLaunched){
        Toast.makeText(this, "1 st launch " , Toast.LENGTH_SHORT).show(); 
        setContentView(R.layout.activity_hls_land);
        }

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.activity_hls_port);
    }
     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.activity_hls_land );
    } 
    }

}

and in the manifest add android:configChanges="orientation" in activity :-

<activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" 
            android:configChanges="orientation"
            >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
          </activity>

Sample layout for activity_hls_port :-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
</LinearLayout>

Sample for landscape mode:-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:orientation="vertical"
    android:rotation="90">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
</LinearLayout>

这篇关于Android设备onConfigurationChanged事件不处理方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆