2013-12-17 108 views
0

我有一个活动,几个按钮和web视图。我想在按下特定按钮时将webview设置为全屏幕,并且在webview被双击时从全屏模式退出。 这是XML:将webview设置为全屏其他控制器

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <ImageButton 
       android:id="@+id/ibNext" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ic_action_previous_item" /> 
      <ImageButton 
       android:id="@+id/ibPrevious" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ic_action_next_item" /> 

       <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="right" > 

       <ImageButton 
        android:id="@+id/ibFullScreen" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_action_full_screen" /> 

       <ImageButton 
        android:id="@+id/ibChapters" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_action_view_as_list" /> 


      </LinearLayout> 


     </LinearLayout> 

     <WebView 
      android:id="@+id/webView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

    </LinearLayout> 

</LinearLayout> 

我已经为全屏按钮的监听器,我只需要知道如何web视图设置为全屏。

感谢

回答

1

做这样

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:id="@+id/lnrOptions" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <ImageButton 
       android:id="@+id/ibNext" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ic_launcher" /> 

      <ImageButton 
       android:id="@+id/ibPrevious" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ic_launcher" /> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="right" 
       android:orientation="horizontal" > 

       <ImageButton 
        android:id="@+id/ibFullScreen" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_launcher" /> 

       <ImageButton 
        android:id="@+id/ibChapters" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_launcher" /> 
      </LinearLayout> 
     </LinearLayout> 

     <WebView 
      android:id="@+id/webView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 
    </LinearLayout> 

</LinearLayout> 

在Java代码中

import android.app.Activity; 
import android.os.Bundle; 
import android.view.GestureDetector; 
import android.view.GestureDetector.OnDoubleTapListener; 
import android.view.GestureDetector.OnGestureListener; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnTouchListener; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.ImageButton; 
import android.widget.LinearLayout; 

public class MainActivity extends Activity implements OnGestureListener { 

    private WebView webView1; 
    private ImageButton ibFullScreen; 
    private LinearLayout lnrOptions; 
    GestureDetector gd; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     webView1 = (WebView) findViewById(R.id.webView1); 
     webView1.setWebViewClient(new WebViewClient()); 
     webView1.loadUrl("http://www.google.com"); 
     ibFullScreen = (ImageButton) findViewById(R.id.ibFullScreen); 
     lnrOptions = (LinearLayout) findViewById(R.id.lnrOptions); 

     gd = new GestureDetector(this); 
     gd.setOnDoubleTapListener(new OnDoubleTapListener() { 

      @Override 
      public boolean onSingleTapConfirmed(MotionEvent e) { 
       return false; 
      } 

      @Override 
      public boolean onDoubleTapEvent(MotionEvent e) { 
       return false; 
      } 
      //UPDATE HERE 
      @Override 
      public boolean onDoubleTap(MotionEvent e) { 
       lnrOptions.setVisibility(View.VISIBLE); 
          getActionBar().show(); 
       return true; 
      } 
     }); 

     webView1.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View arg0, MotionEvent arg1) { 
       return gd.onTouchEvent(arg1); 
      } 
     }); 
      //UPDATE HERE 
     ibFullScreen.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       if (lnrOptions.getVisibility() == View.VISIBLE) { 
        lnrOptions.setVisibility(View.GONE); 
            getActionBar().hide(); 
       } else { 
        lnrOptions.setVisibility(View.VISIBLE); 
       } 
      } 
     }); 

    } 

    @Override 
    public boolean onDown(MotionEvent e) { 
     return false; 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     return false; 
    } 

    @Override 
    public void onLongPress(MotionEvent e) { 

    } 

    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
     return false; 
    } 

    @Override 
    public void onShowPress(MotionEvent e) { 

    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent e) { 
     return false; 
    } 

} 
+0

谢谢您的回答。我用你的答案,现在全屏按钮传播按钮区域上的webview,但ActionBar仍然存在。我怎样才能将WebView传播到ActionBar的区域呢? – rafraph

+0

我更新了我的回答,现在按照您的需要试用它。 –

+0

再次感谢您,但ActionBar的更新适用于API级别11和更高级别。我需要它为API级别7。你知道在API级别7中执行此操作的方式是什么? – rafraph