android-如何在屏幕方向改变时以新尺寸重新加载片段

android-如何在屏幕方向改变时以新尺寸重新加载片段,第1张

概述我有一个片段,其中TextView从文件夹中获取其宽度:值和值端口.并且当方向改变时这些值也会改变,但是片段的UI没有任何作用.实际上,它实际上是从values-port中选择值的,并且:如果设备处于横向模式:调用具有新方向的onConfigurationChanged()作为横向.如果设备处于纵向模式:不调用onC

我有一个片段,其中TextVIEw从文件夹中获取其宽度:

值和值端口.

并且当方向改变时这些值也会改变,但是片段的UI没有任何作用.

实际上,它实际上是从values-port中选择值的,并且:

如果设备处于横向模式:调用具有新方向的onConfigurationChanged()作为横向.

如果设备处于纵向模式:不调用onConfigurationChanged().

当我改变方向时,肯定会调用onConfigurationChanged().

所以,

为什么总是以纵向模式开始?

如何更改UI元素的尺寸.

我在片段中尝试过这样的事情:

@OverrIDepublic voID onConfigurationChanged(Configuration newConfig) {    super.onConfigurationChanged(newConfig);    getVIEw().requestLayout();}

但是不起作用.

我的TextVIEw始终与values-port文件夹中的宽度相同.

编辑:似乎多余,但这是代码:

<activity    androID:name=".FeedActivity"    androID:configChanges="orIEntation|screenSize|keyboardHIDden"    androID:screenorIEntation="portrait" ></activity>@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setRequestedOrIEntation(ActivityInfo.SCREEN_ORIENTATION_UnspecIFIED);}

我知道我可以从清单中删除androID:screenorIEntation =“ portrait”而不是setRequestedOrIEntation(ActivityInfo.SCREEN_ORIENTATION_UnspecIFIED);在活动中,但是想知道此代码的行为.

解决方法:

FeedActivity具有固定的方向,该清单在清单中由androID:screenorIEntation属性指定.因此,当您旋转设备时,它什么也不做.即使可以,您还是要覆盖androID:configChanges.这是@L_419_0@说的话:

Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.

您的清单告诉系统FeedActivity将自行处理方向,screenSize和键盘配置更改. las,您实际上并没有这样做.

因为您说要以XML加载Fragment,所以我假设它看起来像这样:

<?xml version="1.0" enCoding="utf-8"?><FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"             androID:layout_wIDth="match_parent"             androID:layout_height="match_parent">    <fragment androID:layout_wIDth="@dimen/frag_wIDth"              androID:layout_height="match_parent"              androID:tag="your_tag_here"              /></FrameLayout>

要获得您期望的行为,您应该使用

<activity    androID:name=".FeedActivity"    androID:configChanges="screenSize|keyboardHIDden"/>

更新

如果您坚持使用onConfigurationChanged方法更改您的Fragment宽度,则可以这样做:

@OverrIDepublic voID onConfigurationChanged(Configuration newConfig) {    super.onConfigurationChanged(newConfig);    final VIEw vIEw = getVIEw();    if (null != vIEw) {        vIEw.getLayoutParams().wIDth = getResources().getDimensionPixelSize(R.dimen.wIDth);    }}

我建议不要这样做.

总结

以上是内存溢出为你收集整理的android-如何在屏幕方向改变时以新尺寸重新加载片段全部内容,希望文章能够帮你解决android-如何在屏幕方向改变时以新尺寸重新加载片段所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1077825.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存