android webview 横竖屏_横向显示WebView无需旋转屏幕

在不改变设备屏幕方向的情况下,如何使WebView内容横屏显示。一种方法是创建自定义WebView,通过重写draw方法,实现内容旋转90度,以达到在保持设备竖屏时WebView内容横向展示的效果。
摘要由CSDN通过智能技术生成

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.

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.

Is that possible?

解决方案

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:

android:id="@+id/myview"

android:layout_width="match_parent"

android:layout_height="wrap_content" >

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值