【问题标题】:How I hide a WebView?如何隐藏 WebView?
【发布时间】:2010-11-03 11:16:29
【问题描述】:

我需要在加载 Web 内容时隐藏 WebVew。我试图用这样的其他视图来做到这一点:

<WebView
    android:scrollbars="none" 
    android:id="@+id/id_webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

<View
    android:visibility="gone"
    android:scrollbars="none" 
    android:id="@+id/id_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

当我想隐藏 WebView 时,我将 View 的可见性更改为“可见”(View.setVisibility(View.VISIBLE);)。但是 View 没有覆盖 WebView 也没有隐藏。我需要在加载 WebView 时将视图放在前面。

【问题讨论】:

    标签: android webview


    【解决方案1】:

    虽然我觉得这种方法很奇怪,但您应该检查这些视图的父容器。

    如果是LinearLayout,难怪View不覆盖WebView。 如果您想使用 Layouts,请尝试使用 RelativeLayout 并以相同的方式对齐元素,例如,添加到两个视图:

    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    

    另一个变体(更正确的 IMO)是使用 ViewSwitcher 或 ViewFlipper。它使用 showNext()、showPrevious()(在 ViewFlipper 中)和 getNextView()(在 ViewSwitcher 中)方法在其子级之间切换。真的很容易实现和使用。查找一些示例。

    只是一个小提示:

    <!-- ViewSwitcher or ViewFlipper -->
    <ViewSwitcher
    
        android:id="@+id/view_switcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <View
            android:scrollbars="none" 
            android:id="@+id/id_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
        <WebView
            android:scrollbars="none" 
            android:id="@+id/id_webview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
    </ViewSwitcher>
    

    并且,在您的代码中:

    // This will hide currently displayed and reveal next
    ((ViewSwitcher) findViewById(R.id.view_switcher)).getNextView(); 
    
    // Or, in case of ViewFlipper:
    // This will hide currently displayed and reveal next
    ((ViewFlipper) findViewById(R.id.view_switcher)).showNext();
    

    两者的区别在于Switcher只能有2个孩子,并且有一个工厂用于创建视图。

    附:混合了两位动画师,编辑了帖子。

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 2020-09-04
      • 2012-06-27
      • 1970-01-01
      相关资源
      最近更新 更多