简体   繁体   English

横向和纵向定向模式问题

[英]landscape and portrait orientation mode issue

I am running an application in which I'm running an audio when it is clicked on image, while audio is being played and i changed the mode from landscape to portrait or from portrait to landscape and click on image, it plays another audio clip. 我正在运行一个应用程序,当我点击图像时我正在运行音频,同时正在播放音频,我将模式从横向更改为纵向或从纵向更改为横向,然后单击图像,它将播放另一个音频剪辑。

Example: 例:

2 audios are being played now. 现在正在播放2个音频。 But this issue does not occur by clicking the images in the same mode. 但是,通过单击相同模式下的图像不会发生此问题。

Is there any way to stop the previous activity when mode is changed? 更改模式时有没有办法停止上一个活动?

For playing audio files just use Service don't play inside Activity ,so that you can stop Service so that song can be Stopped. 对于播放音频文件,只需使用Service不在Activity内播放,这样就可以停止服务,以便歌曲可以停止。 Then you should focus on how you are going to handle your Orientation 然后你应该专注于你将如何处理你的方向

  1. Recreating Activity 重建活动
  2. Handle orientation changes yourself. 处理方向自行改变。

My best Advice is that you should handle yourself.For that you should use two thing : 我最好的建议是你应该自己处理。为此,你应该使用两件事:

  1. Add
    <activity android:name=".CurrentActivity android:configChanges="keyboardHidden|orientation"> </activity>

2.Call 2.Call

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
  if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
          Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
   } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
     }
}

If orientation is changed you should handle Service as you wants. 如果方向已更改,您应该根据需要处理服务。

Add this code in AndroidManifest with the current activity where you facing problem, but this will stop your app drawables change when you change orientation, it will use the current orientation as static for both mode, but still try and let me know. AndroidManifest中添加此代码,其中包含您遇到问题的当前活动,但这会在您更改方向时停止更改app drawables,它将使用当前方向作为两种模式的静态,但仍尝试告诉我。

<activity android:name=".CurrentActivity
 android:configChanges="keyboardHidden|orientation">
        </activity>

I have tried @venky's answer but my layout is disturbed :( 我试过@ venky的答案,但我的布局受到干扰:( 在此输入图像描述

在此输入图像描述

and for portrait also when orientation is changed, the buttons also don't work :( 而对于肖像,当方向改变时,按钮也不起作用:(

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_prt"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="vertical" >

</LinearLayout>







<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="28dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/pause1"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:src="@drawable/pause" />




    <ImageView
        android:id="@+id/play1"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:src="@drawable/play" />







    <ImageView
        android:id="@+id/home1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="165dp"
        android:src="@drawable/home" />

</LinearLayout>




<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="315dp"
    android:orientation="vertical" >


    <ImageView
        android:id="@+id/kalma1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="20dp"
        android:src="@drawable/kalma1" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal" >



    <ImageView
        android:id="@+id/next1"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:src="@drawable/forward" />

</LinearLayout>

is my xml for portrait and 是我的xml肖像和

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_lndscp"
android:orientation="vertical" >

<ImageView
    android:id="@+id/kalma1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="33dp"
    android:src="@drawable/kalma1" />


<ImageView
    android:id="@+id/next1"
    android:layout_width="30dp"
    android:layout_height="20dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="10dp"
    android:src="@drawable/forward" />



<ImageView
    android:id="@+id/play1"
    android:layout_width="30dp"
    android:layout_height="20dp"
    android:layout_marginLeft="65dp"
    android:layout_marginTop="41dp"
    android:src="@drawable/play" />




<ImageView
    android:id="@+id/pause1"
    android:layout_width="30dp"
    android:layout_height="20dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="41dp"
    android:src="@drawable/pause" />



<ImageView
    android:id="@+id/home1"
    android:layout_width="30dp"
    android:layout_height="20dp"
    android:layout_alignBottom="@+id/play1"
    android:layout_alignRight="@+id/kalma1"
    android:src="@drawable/home" />

</RelativeLayout>

for landscape 对于景观

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM