gpt4 book ai didi

android - 在 Android 中更改方向时布局中断

转载 作者:行者123 更新时间:2023-11-29 00:31:35 27 4
gpt4 key购买 nike

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

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

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

如果我从 list 中删除这部分 |screenSize,它工作正常,但 layout-land 不起作用,纵向布局在两个方向上都使用。

这是 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;
}

}

list .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>

最佳答案

我通过从 onCreate 中拉出 setContentViewtry/catch block 并将其放入 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 中发现了另一个问题。当我按下关于按钮退出此 Activity 并运行 MainActivity 时,应用程序停止。怎么了?

关于android - 在 Android 中更改方向时布局中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15331643/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com