横向显示WebView而无需旋转屏幕 [英] Display WebView in landscape WITHOUT screen rotation

查看:1065
本文介绍了横向显示WebView而无需旋转屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些WebView内容,以横向格式看起来会更好.我找到了一些解决方案,这些解决方案使我能够在显示WebView时强制横向显示,从而实现了目标,但是我不喜欢这种方式.

I have some content for a WebView that would look better in landscape format. I've found solutions which would enable me to force landscape orientation when displaying the WebView, which achieves the goal, but in a way that I don't like.

特别是,我认为在没有警告和要求的情况下突然更改实际屏幕旋转并不是一种很棒的用户体验,因此,状态栏等使一切旋转在眼前从顶部移动到另一侧.

In particular, I don't think it's a great user experience to have the actual screen rotation changed suddenly, without warning and without having requested this, so that everything sort of spins in front of your eyes, with the status bar etc moving from top to side.

我希望实际的屏幕旋转保持不动,因此如果当前设置的话,它将保持纵向.所有需要发生的是,WebView中的页面以横向显示在侧面.

I'd prefer that the actual screen rotation is left alone, so that it remains in portrait orientation if that is what it is presently set. All that needs to happen is that the page within the WebView is displayed sideways, in landscape orientation.

有可能吗?

推荐答案

您可以尝试创建自定义WebView,例如:

You can try create a custom WebView like:

public class VWebView extends WebView {
    final boolean topDown = true;

    public VWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void draw(Canvas canvas) {
        if (topDown) {
            canvas.translate(getHeight(), 0);
            canvas.rotate(90);
        } else {
            canvas.translate(0, getWidth());
            canvas.rotate(-90);
        }
        canvas.clipRect(0, 0, getWidth(), getHeight(), android.graphics.Region.Op.REPLACE);
        super.draw(canvas);
    }
}

XML:

<com.my.package.VWebView
    android:id="@+id/myview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</com.my.package.VWebView>

这篇关于横向显示WebView而无需旋转屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆