【问题标题】:Layout broken when change orientation in Android在 Android 中更改方向时布局损坏
【发布时间】:2013-03-11 05:16:42
【问题描述】:

我正在开发用于显示频道直播的应用程序,它默认以横向模式启动,我可以从我的选项菜单将其更改为纵向,反之亦然

现在我在清单中使用此代码并将其放入该活动中 android:configChanges="keyboard|keyboardHidden|orientation|screenSize"

当我启动应用程序时,它工作正常,但是当我将方向更改为纵向时,应用程序挂起并且选项菜单不会像往常一样消失。

如果我从清单中删除了这部分 |screenSize,它工作正常,但布局区域不起作用,并且在两个方向都使用纵向布局。

这是 LiveActivity.java

package com.shadatv.shada;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;

import android.view.Menu;
import android.view.MenuItem;

import android.view.WindowManager;

import android.widget.VideoView;

public class LiveActivity extends Activity {
    VideoView videoView;

    private AlertDialog.Builder errorDialog;
    String httpLiveUrl = "http://38.96.148.147:1935/live/livestream/playlist.m3u8";

    // =============================================================================

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        setContentView(R.layout.live);
        try {
            videoView = (VideoView) findViewById(R.id.myVideoView);
            videoView.setVideoURI(Uri.parse(httpLiveUrl));
            videoView.requestFocus();

            videoView
                    .setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            // TODO Auto-generated method stub

                            videoView.seekTo(1000);
                            videoView.start();

                        }
                    });

        } catch (Exception e) {
            errorDialog = new AlertDialog.Builder(this);
            errorDialog.setMessage("مشكلة في البث");
            errorDialog.setCancelable(true);
            errorDialog.setNeutralButton("Ok",
                    new DialogInterface.OnClickListener() {
                        // click listener on the alert box
                        public void onClick(DialogInterface arg0, int arg1) {

                        }
                    });
            AlertDialog errorStream = errorDialog.create();
            errorStream.show();
        }
    }

    // =============================================================================

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.clear();
        getMenuInflater().inflate(R.menu.activity_shada, menu);
        MenuItem fullItem = menu.findItem(R.id.fullScreen);
        MenuItem smallItem = menu.findItem(R.id.smallScreen);

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            fullItem.setVisible(false);
            smallItem.setVisible(true);
        } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            smallItem.setVisible(false);
            fullItem.setVisible(true);
        }
        return super.onPrepareOptionsMenu(menu);
    }

    // =============================================================================

    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            getWindow().clearFlags(
                    WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            // setContentView(R.layout.activity_shada);
        } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            // setContentView(R.layout.activity_shada1);
        }
    }

    // =============================================================================

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.about: {
            // TODO Auto-generated method stub
            startActivity(new Intent("com.shadatv.MainActivity"));
        }
            break;

        case R.id.smallScreen: {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
            break;

        case R.id.fullScreen: {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
            break;
        }
        return true;
    }

}

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.shadatv.shada"
    android:versionCode="3"
    android:versionName="1.0.2" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/shada"
        android:label="@string/app_name"

        >       
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name=".LiveActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:exported="false"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

            <intent-filter>
                <action android:name="com.shadatv.LiveActivity" />

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


</manifest>

【问题讨论】:

    标签: android layout orientation landscape


    【解决方案1】:

    我通过从onCreate 中拉出setContentViewtry/catch 块并将其放入onConfigChanged 来解决它

    package com.shadatv.shada;
    
    import android.media.MediaPlayer;
    import android.net.Uri;
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.AlertDialog;
    
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.ActivityInfo;
    import android.content.res.Configuration;
    
    import android.view.Menu;
    import android.view.MenuItem;
    
    import android.view.WindowManager;
    
    import android.widget.VideoView;
    
    public class LiveActivity extends Activity {
        VideoView videoView;
    
        private AlertDialog.Builder errorDialog;
        String httpLiveUrl = "http://38.96.148.147:1935/live/livestream/playlist.m3u8";
    
        // =============================================================================
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            // if (getResources().getConfiguration().orientation ==
            // Configuration.ORIENTATION_LANDSCAPE) {
            // getWindow().clearFlags(
            // WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            // WindowManager.LayoutParams.FLAG_FULLSCREEN);
            // }
            // setContentView(R.layout.live);
    
        }
    
        // =============================================================================
    
        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            menu.clear();
            getMenuInflater().inflate(R.menu.activity_shada, menu);
            MenuItem fullItem = menu.findItem(R.id.fullScreen);
            MenuItem smallItem = menu.findItem(R.id.smallScreen);
    
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                fullItem.setVisible(false);
                smallItem.setVisible(true);
            } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                smallItem.setVisible(false);
                fullItem.setVisible(true);
            }
            return super.onPrepareOptionsMenu(menu);
        }
    
        // =============================================================================
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            setContentView(R.layout.live);
            try {
                videoView = (VideoView) findViewById(R.id.myVideoView);
                videoView.setVideoURI(Uri.parse(httpLiveUrl));
                videoView.requestFocus();
    
                videoView
                        .setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    
                            @Override
                            public void onPrepared(MediaPlayer mp) {
                                // TODO Auto-generated method stub
    
                                videoView.seekTo(1000);
                                videoView.start();
    
                            }
                        });
    
            } catch (Exception e) {
                errorDialog = new AlertDialog.Builder(this);
                errorDialog.setMessage("مشكلة في البث");
                errorDialog.setCancelable(true);
                errorDialog.setNeutralButton("Ok",
                        new DialogInterface.OnClickListener() {
                            // click listener on the alert box
                            public void onClick(DialogInterface arg0, int arg1) {
    
                            }
                        });
                AlertDialog errorStream = errorDialog.create();
                errorStream.show();
            }
    
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                getWindow().clearFlags(
                        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);    
            } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                getWindow().setFlags(
                        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            }
        }
    
        // =============================================================================
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.about:
                // TODO Auto-generated method stub
                startActivity(new Intent("com.shadatv.MainActivity"));
                break;
    
            case R.id.smallScreen:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;
    
            case R.id.fullScreen:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
            }
            return true;
        }
    
    }
    

    但我在 optionsMenu 发现了另一个问题。当我按下 about 按钮退出此活动并运行 MainActivity 时,应用程序停止。怎么了?

    【讨论】:

    • 为什么你不使用这种方式调用活动。 Intent intent = new Intent(LiveActivity .this,Main.class);开始活动(意图);
    • 看起来这个答案最后提出了一个新问题。我们更喜欢不包含该内容的问题 - 是否可以删除结尾位并让答案仍然有用,还是完全删除答案更好?
    【解决方案2】:

    当您在 AndroidManifest 中写入 android:configChanges="keyboardHidden|orientation" 时,您是在告诉 Android:“不要在拔出键盘或旋转手机时进行默认重置;我想自己处理。

    所以当屏幕方向改变时,纵向布局不会设置在您的活动上。 你为什么使用android:configChanges="keyboardHidden|orientation

    【讨论】:

    • 因为我从 optionsMenu 动态地从横向转换为纵向,反之亦然
    • 所以我认为你必须 setContentView(R.layout.portrait);动态点击。
    • 谢谢我解决了它,但我在我的答案中解释了另一个问题。你能帮我吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多