Android:如何在scrollview中添加webview + 1个视图?

时间:2015-01-20 08:05:41

标签: android webview

我需要滚动webview。多数民众赞成。但是当webview滚动到最后时,我想再一个视图看起来像webview,而另一个视图在单个滚动视图中。怎么做得好? 考虑将webview置于scrollview中是不正确的。

P.S。换句话说,它看起来应该像带有页脚的webview。页脚是常规的android视图。

2 个答案:

答案 0 :(得分:1)

一个粗略的方法可以是:将UIWebView放在UIScrollView中并根据您的需要设置consentSize(即在UIWebView内容的末尾添加另一个您选择的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,但这可能是一个很好的起点。

答案 1 :(得分:0)

只需在滚动视图中添加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>