全屏使用 WebView 和relativelayout 时出现奇怪的行为
编辑: 以下是显示问题的视频:www.youtube.com/watch?v=5ZZsuMH5T9k我真的被困在这里了 :/
——
我有一个由 webview 和底部按钮组成的活动。我在 onCreate
中使用以下代码将 Activity 的窗口设置为全屏:
@Override
public void onCreate(Bundle savedInstanceState) {
InstaFetchApplication.applyCurrentTheme(this);
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
requestWindowFeature(Window.FEATURE_PROGRESS);
if (UserPreferences.getIsFullScreenReadingEnabled(this)) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
// this just do this: window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ActivityUtils.toggleFullScreen(this, true);
}
setContentView(R.layout.view_article);
// ...
}
问题是大多数时候 webview 似乎将按钮“推”出了屏幕。这是屏幕截图:
(更大版本)
我期望看到的(有时我确实看到的)在这里:
(较大版本)
这是我正在使用的布局(请注意,在我的应用程序中,顶部布局必须是relativelayout):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Click me!"
/>
<WebView
android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/button"
/>
</RelativeLayout>
我还注意到,如果您使用系统设置关闭设备上的所有动画,问题是消失了(虽然当整个视图有上边距然后跳回到正确的位置时就会出现这个问题)。
有没有人遇到过类似的问题并知道如何解决?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最后我找到了答案。在窗口中添加 WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS 标志解决了我的问题。
请参阅这篇文章:flag_fullscreen + windowNoTitle == 按钮焦点错误。
Finally I found the answer. Adding
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
flag to the window solved my problem.See this post: flag_fullscreen + windowNoTitle == button focus bug.
我刚刚在 Hello WebView 教程中添加了一个按钮,以及所有内容看起来似乎是对的。然后我将
main.xml
从LinearLayout
转换为RelativeLayout
,一切看起来仍然正确。我认为你把事情搞得太复杂了。 :-) 尝试从onCreate()
中删除大部分初始化代码,然后在 XML 中指定它。这是我的项目。
AndroidManifest.xml:
HelloWebViewActivity.java:
main_ll.xml(LinearLayout):
main_rl.xml(RelativeLayout):
strings.xml:
I just added a button to the Hello WebView tutorial, and everything seems to look right. Then I converted
main.xml
fromLinearLayout
toRelativeLayout
, and everything still seems to look right. I think you are making things too complicated. :-) Try removing most of that initialization code fromonCreate()
and just specify it in the XML.Here is my project.
AndroidManifest.xml:
HelloWebViewActivity.java:
main_ll.xml (LinearLayout):
main_rl.xml (RelativeLayout):
strings.xml:
尝试将 Web 视图与父级顶部对齐,并将按钮与父级底部对齐, 使其位于 Web 视图下方。我发现有时,如果没有真正的原因,如果元素没有全部按线性顺序串在一起(锚定在屏幕的顶部或底部),则它们不会完全正确排列。
我认为您没有重写 onMeasure() 方法并更改按钮的测量高度。我在应用程序开发过程中通过在布局阶段错误计算元素的高度而产生了许多(太多)错误,并且花了几个小时努力调试布局定义代码,结果发现它很好,但高度计算是将无效数据输入布局的渲染阶段。
Try to align the web view to the parent top, and align the button to the parent bottom and to be below the web view. I have found times when, for no real reason, the elements don't quite line up correctly if they are not all strung together in a linear sequence either anchored at the top or at the bottom of the screen.
I presume that you are not overriding the onMeasure() method and changing the measured height of the button. I've created numerous (too many) bugs during development of my apps by miscalculating the height of an element during the layout phase and have spent hours struggling with debugging the layout definition code only to find that it was fine, but the height calculation was feeding invalid data into the layout's rendering phase.
除非
RelativeLayout
是绝对必要的,否则您可以更改为LinearLayout
:它应该会提供更好的结果。
Unless the
RelativeLayout
is absolutely necessary, You could change to aLinearLayout
:It should give a better result.
要隐藏标题栏,我使用:
在 Galaxy S 2 (Android 2.3.3) 和华为 U8180 X1 (Android 2.2.2) 上进行测试,并且与您的相对布局配合良好。
从屏幕截图来看,我的印象是问题在于您请求的进度指示器不完整。关闭动画后问题消失的事实也支持了这一理论。
To hide the title bar I use:
Tested on a Galaxy S 2 (Android 2.3.3) and Huawei U8180 X1 (Android 2.2.2) and worked fine with your relative layout.
From the screenshot I have the impression that the problem lies rather with the incomplete progress indicator you requested. The fact that the problem disappears when turning off the animations also supports this theory.
你可以试试这个。顶部布局仍然是
RelativeLayout
,与您的唯一区别是 LinearLayout 包裹了所有内容:在
LinearLayout
中,WebView
占据整个窗口(和父窗口)的 90%
和按钮剩余的10%
。两者的高度都设置为0px
,因此重量可以为它们完成工作。解析此 xml 的成本微不足道。在 Java 代码中我做了:
You may try this. The top layout is still
RelativeLayout
, the only difference with yours is the LinearLayout is wrapping up everything:In the
LinearLayout
,WebView
occupies90%
of entire window(and parent) and the button remaining10%
. The height is set to0px
for both, so the weight can do the work for both of them. The cost for the parsing this xml is insignificant.And in Java code I did:
下面的代码可能会帮助你,
below code might help you,