如何检测Android中的布局方向变化?
我刚刚实施了方向更改功能。
当布局从纵向变为横向(反之亦然)时,如何检测方向更改事件何时完成。
我尝试使用OrientationEventListener
,但它似乎不是一个好主意。
我只是想在布局方向改变时才被通知。 怎么做?
- 如何检查Android中是否存在资源
- 无法更新Android Studio – 只能下载
- Android:如何获取代码中的属性值?
- 如何在Android Studio中的模拟器上安装一个apk?
- INSTALL_FAILED_DUPLICATE_PERMISSION … C2D_MESSAGE
使用Activity的onConfigurationChanged方法。 看下面的代码:
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Checks the orientation of the screen 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(); } }
你也必须编辑适当的 元素在你的清单文件中包含android:configChanges只要看看下面的代码:
<activity android:name=".MyActivity" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name">
注意:使用Android 3.2(API级别13)或更高版本时,设备在纵向和横向之间切换时,“屏幕大小”也会改变。 因此,如果要在开发API级别13或更高版本时防止由于方向更改而导致运行时重新启动,则必须为API级别13或更高版本声明android:configChanges =“orientation | screenSize” 。
希望这会帮助你… 🙂
要在layout-land文件夹中加载布局,意味着您有两个单独的布局,则必须在onConfigurationChanged
方法中创建setContentView
。
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Checks the orientation of the screen if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { setContentView(R.layout.yourxmlinlayout-land); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ setContentView(R.layout.yourxmlinlayoutfolder); } }
如果只有一个布局,则不需要在此方法中创建setContentView。 只是
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); }
只是想告诉你一个方法来保存你的Bundle后onConfigurationChanged:
在课后创建新的Bundle:
public class MainActivity extends Activity { Bundle newBundy = new Bundle();
接下来,在“protected void onCreate”之后添加:
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { onSaveInstanceState(newBundy); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { onSaveInstanceState(newBundy); } } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBundle("newBundy", newBundy); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); savedInstanceState.getBundle("newBundy"); }
如果你添加这个所有你的crated类到MainActivity将被保存。
但最好的方法是在你的AndroidManifest中添加这个:
<activity name= ".MainActivity" android:configChanges="orientation|screenSize"/>
覆盖Activity的方法onConfigurationChanged
使用这种方法
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { Toast.makeText(getActivity(),"PORTRAIT",Toast.LENGTH_LONG).show(); //add your code what you want to do when screen on PORTRAIT MODE } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { Toast.makeText(getActivity(),"LANDSCAPE",Toast.LENGTH_LONG).show(); //add your code what you want to do when screen on LANDSCAPE MODE } }
别忘了在你的Androidmainfest.xml中添加这个
android:configChanges="orientation|screenSize"
喜欢这个
<activity android:name=".MainActivity" android:theme="@style/Theme.AppCompat.NoActionBar" android:configChanges="orientation|screenSize"> </activity>
- Android Studio 3.0风味维度问题
- Android SDK:获取原始预览相机图像而不显示它
- Android:什么是android.R.id.content用于?
- Android Facebook SDK 3.0在login时给出“remote_app_id与存储的ID不匹配”
- 为什么我的three.js场景中的焦点仍然集中在相机的视angular,但只在Chrome的Android?
- MultiAutoCompleteTextView和AutoCompleteTextView之间的区别
- 如何制作一个圆形的button?
- 使用AssetManager.list列出子目录中的资产
- 在Eclipse的Android设备中没有看到Nexus7