2013-01-20 25 views
1

我有一個webview,我希望能夠以編程方式更改顯示的頁面的方向。 當用戶通過在Manifest文件中設置此項來改變手機的方向時,我禁止再次重新創建活動。Android Webview以編程方式更改顯示的網頁的方向

android:screenOrientation="nosensor" 
android:configChanges="orientation|screenSize|keyboardHidden" 

但是在我的一個選項卡,我想以編程方式更改網頁的方向,當用戶改變它。所以我想聽聽手機定位的變化,並相應地改變頁面視圖的變化。目前,當手機的方向改變爲橫向時,網頁仍然以縱向顯示。

有人可以幫忙嗎?

+0

你爲什麼要這麼做? –

+0

我編輯了更多的細節問題@RaghavSood –

回答

1

如果想僅旋轉的WebView,沒有別的,創建一個自定義的WebView這樣的:

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

    public VerticalWebView(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); 
    } 
} 

(更改topDown爲false,如果你想旋轉的其他方式)

現在簡單地在你的XML中使用它如下:

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

請記住,這只是旋轉視圖顯示在s creen和任何鏈接等將無法工作,因爲它不會將觸摸座標重新映射到相應點的新位置。

+0

我希望能夠在運行時更改旋轉。你所提供的是一勞永逸的。 –

+0

Raghav ...你如何讓XML工作,我不斷得到充氣例外 –

相關問題