代码之家  ›  专栏  ›  技术社区  ›  Alex

如何隐藏WebView?

  •  3
  • Alex  · 技术社区  · 15 年前

    我需要在WebVew加载其Web内容时隐藏它。我试着用其他的视图来做这个:

    <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.setvisibility(view.visible);)。但是这个视图不包括WebView,也不隐藏。我需要在加载WebView时将视图放在前面。

    1 回复  |  直到 15 年前
        1
  •  6
  •   adaliszk    9 年前

    尽管我发现这种方法很奇怪,但是您应该检查那些视图的父容器。

    如果是线性布局,难怪视图不包括WebView。 如果要使用用户布局,请尝试使用相对布局和相同对齐元素,例如,向两个视图添加:

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

    另一个变种(和更正确的IMO)是使用取景器或取景器。它使用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();
    

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

    P.S.混合了两个动画师,编辑后。