
我有一个屏幕,当设备是lanscape和portrait时显示不同的用户界面.它在设备为纵向时显示ListVIEw,在设备为lanscape时显示GrIDVIEw.
它看起来像youtube应用程序或谷歌播放商店应用程序.
感谢您阅读和回答我的问题.
编辑:我想添加有关我的问题的更多详细信息:
>我正在使用片段来管理此视图.
>我需要保存所有数据并在此视图中旋转(避免重新加载大量数据).
>我尝试在清单上添加android:configChanges并在此片段中使用onConfigurationChanged.但这并不成功.
我希望看到特定的例子或详细的解决方案.
但任何答案都是适当的.
谢谢
解决方法:
开发此功能有两件事.
首先,您必须制作两种不同的布局:
res|- layout |- mylayout.xml|- layout-land.xml |- mylayout.xml在layout / mylayout.xml文件中,包含您需要它的ListVIEw,并在layout-land / mylayout.xml中包含您的GrIDVIEw.
当您调用setContentVIEw时,AndroID将根据当前屏幕方向自动选择正确的布局.
@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.mylayout); mListVIEw = (ListVIEw) findVIEwByID(R.ID.myListvIEw); mGrIDVIEw = (GrIDVIEw) findVIEwByID(R.ID.mygrIDvIEw); // !!!!!!! // mListVIEw will be null in landscape // mGrIDVIEw will be null in portrait // !!!!!!!}setContentVIEw(R.layout.mylayout);现在,在代码中,您需要检查if / else语句的方向.
要检测您的方向,请遵循以下状态:
>在res / values / bool.xml中创建一个文件
<resources> <bool name="isLandscape">false</bool></resources>>在res / values-land / bool.xml中创建一个文件
<resources> <bool name="isLandscape">true</bool></resources>>现在,在您的活动中,您可以在每次需要时轻松测试,如果您处于横向或纵向模式并应用相应的 *** 作
boolean landscape = getResources().getBoolean(R.bool.isLandscape);if (landscape) { // do something with your GrIDVIEw} else { // do something with your ListVIEw}要回复您的修改:
碎片与活动非常相似.
如果要在方向更改期间保存数据,请遵循official documentation on this topic,尤其是Retaining an Object During a Configuration Change中的部分.
您不应该使用androID:configChangesin Manifest,如in the documentation所述:
总结Handling the configuration change yourself can make it much more
difficult to use alternative resources, because the system does not
automatically apply them for you. This technique should be consIDered
a last resort when you must avoID restarts due to a configuration
change and is not recommended for most applications.
以上是内存溢出为你收集整理的设备更改方向android时更改用户界面全部内容,希望文章能够帮你解决设备更改方向android时更改用户界面所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)