将任意数量的WebView添加到ScrollView

时间:2011-09-09 10:45:19

标签: android webview xamarin.android horizontalscrollview

我有这个问题:

我在Horizo​​ntalScrollVew中添加了3个WebView。

XML

<?xml version="1.0" encoding="utf-8"?> 
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/Scroll"
  android:fillViewport="true"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
  <LinearLayout android:id="@+id/container"
      android:orientation="vertical" android:layout_width="fill_parent"
      android:layout_height="fill_parent"></LinearLayout>

</HorizontalScrollView>

代码

protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Main);

        LinearLayout container = FindViewById<LinearLayout>(Resource.Id.container);
        ScrollView  scrollView = FindViewById<HorizontalScrollView>(Resource.Id.Scroll);
      scrollView.HorizontalScrollBarEnabled = true;
        scrollView.VerticalScrollBarEnabled = false;
        int top = 0;
        int left = 0;


        WebView WebView1 = new WebView(this);
        WebView1.LoadUrl("http://...");
        WebView1.HorizontalScrollBarEnabled = false;
        WebView1.VerticalScrollBarEnabled = false;                    
        this._layoutParams = null;
        this._layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);
        _layoutParams.SetMargins(left, top, 0, 0);          
        container.AddView(WebView1, _layoutParams);            

        WebView WebView2 = new WebView(this);
        WebView2.LoadUrl("http://...");
        WebView2.HorizontalScrollBarEnabled = false;
        WebView2.VerticalScrollBarEnabled = false;
        WebView2.SetMinimumWidth(600);
        WebView2.SetMinimumHeight(500);              
        this._layoutParams = null;
        this._layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent,LinearLayout.LayoutParams.WrapContent);
        left += 300;
        _layoutParams.SetMargins(left, top, 0, 0);         
        container.AddView(WebView2, this._layoutParams);

        WebView WebView3 = new WebView(this);
        WebView3.LoadUrl("http://...");
        WebView3.HorizontalScrollBarEnabled = false;
        WebView3.VerticalScrollBarEnabled = false;
        WebView3.SetMinimumWidth(600);
        WebView3.SetMinimumHeight(500);
        this._layoutParams = null;
        this._layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent,LinearLayout.LayoutParams.WrapContent);
        left += 600;
        _layoutParams.SetMargins(left, top, 0, 0);         
        container.AddView(WebView3, this._layoutParams);   
    } 

但是没有显示WebViews。请告诉我如何正确地将一些WebView添加到Horizo​​ntalScrollView并在fisicalDisplay上显示它们。

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以尝试将网络视图放入线性布局。 但由于webview是可滚动的,我认为你不能实际滚动ScrollView - 你必须以编程方式滚动它。或者您可以使用ViewFlipper,这也允许您使用自定义动画。