【问题标题】:Android: how to put webview + 1 more view in scrollview?Android:如何在滚动视图中放置 webview + 1 个视图?
【发布时间】:2015-03-18 09:14:46
【问题描述】:

我需要滚动网页视图。那很简单。但是,当 webview 滚动到最后时,我希望再显示一个视图,就像 webview 一样,并且另一个视图在单个滚动视图中。如何正确地做到这一点? 考虑将 webview 放在 scrollview 中是不正确的。

附:换句话说,它应该看起来像有一个带页脚的 web 视图。页脚是常规的 android 视图。

【问题讨论】:

  • 认为将 webview 放入 scrollview 是不正确的。
  • 你可以去自定义WebView
  • according to Android design documents you should never put a scrolling container inside a scrolling container if they scroll the same direction. It's not meant to handle such a thing.
  • 我知道。不过,我希望本机 android 视图能够像它与 webview 一起在同一个滚动容器中一样运行。该怎么做?

标签: android webview


【解决方案1】:

只需在 Scroll View 中添加 Webview 它会自动禁用 WebView 的滚动,请参阅此演示

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/svc"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:background="@color/white">

        <WebView
            android:id="@+id/wv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</ScrollView>

【讨论】:

  • 认为将 webview 放入 scrollview 是不正确的。
【解决方案2】:

大致的方法可以如下:将您的 UIWebView 放在 UIScrollView 中并根据您的需要设置许可大小(即在 UIWebView contentSize 的末尾附加您选择的另一个 UIView):

#import "ViewController.h"

@interface ViewController () {
    UIScrollView *aScrollView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    aScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:aScrollView];

    UIWebView *aWebView = [[UIWebView alloc] initWithFrame:self.view.frame];
    aWebView.delegate = self;
    [aWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com"]]];
    [aScrollView addSubview:aWebView];
}

- (void)webViewDidFinishLoad:(UIWebView *)webview
{
    CGFloat yourWebViewContentHeight = [[webview stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];

    [aScrollView setContentSize: CGSizeMake(aScrollView.frame.size.width, yourWebViewContentHeight + 80)];

    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, yourWebViewContentHeight, self.view.frame.size.width, 80)];
    aView.backgroundColor = [UIColor redColor];

    [aScrollView addSubview:aView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

要获得更精细的粒度,您必须处理 scrollviewdelegate 但这可能是一个很好的起点。

【讨论】:

  • 糟糕,我忘了说这个问题是关于 android 的。我的错。
  • 你应该删除这个答案。问题是针对 Android,而不是针对 iOS。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-26
相关资源
最近更新 更多