web视图,滚动型视图、web

2023-09-12 01:52:00 作者:掐指一算、此奶大胸之罩

我必须把的WebView进入滚动型。但我必须把一些观点在同一个web视图滚动视图之前。所以它看起来是这样的:

I have to place WebView into ScrollView. But I have to put some views into the same scrollview before webview. So it looks like this:

<ScrollView
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<LinearLayout
    android:id="@+id/articleDetailPageContentLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
  <LinearLayout
      android:id="@+id/articleDetailRubricLine"
      android:layout_width="fill_parent"
      android:layout_height="3dip"
      android:background="@color/fashion"/>

  <ImageView
      android:id="@+id/articleDetailImageView"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:adjustViewBounds="true"
      android:scaleType="fitStart"
      android:src="@drawable/article_detail_image_test"/>

  <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:padding="5dip"
      android:text="PUBLISH DATE"/>

  <WebView
      android:id="@+id/articleDetailContentView"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="@color/fashion"
      android:isScrollContainer="false"/>
</LinearLayout>

我收到从后端的一些HTML信息。它没有任何身体或头部标记,由&LT所包围的数据; P&GT; &LT; H4&GT; 或一些其他标签。此外,它拥有&LT; IMG&GT; 标签在那里。有时,图片太宽了当前屏幕的宽度。所以,我在HTML中的开头加了一些CSS。所以,我的数据加载到的WebView是这样的:

I'm getting some HTML info from backend. It has no any body or head tags, just data surrounded by <p> or <h4> or some other tags. Also it has <img> tags in there. Sometimes pictures are too wide for current screen width. So I added some css in the begining of HTML. So I loads data to webview like this:

private final static String WEBVIEW_MIME_TYPE = "text/html";
    private final static String WEBVIEW_ENCODING = "utf-8";
String viewport = "<head><meta name=\"viewport\" content=\"target-densitydpi=device-dpi\" /></head>";
        String css = "<style type=\"text/css\">" +
            "img {width: 100%;}" +
            "</style>";
        articleContent.loadDataWithBaseURL("http://", viewport + css + articleDetail.getContent(), WEBVIEW_MIME_TYPE,
            WEBVIEW_ENCODING, "about:blank");

有时候,当页面加载,滚动视图滚动到放置在web视图开始。我不知道如何解决这个问题。

Sometimes when page loaded, scrollview scrolls to place where webview begins. And I don't know how to fix that.

此外,有时有出现的WebView内容后,巨大的白色空白区域。我也不知道该怎么做这一点。

Also, sometimes there is huge white empty space appears after webview content. I also don't know what to do with that.

有时候,滚动型的滚动条开始抽搐随机而我滚动...

Sometimes scrollview's scrollbars starts twitch randomly while I scrolling...

我知道,这是不正确的地方的WebView为滚动视图,但似乎我没有别的最佳的选择。任何人都可以提出分辩方式把所有的意见和所有的HTML内容的WebView?

I know that it's not right to place webview into scrollview, but it seems like I have no other choise. Could anyone suggest rigth way to place all views and all HTML content to webview?

推荐答案

更新二零一四年十一月一十三日:由于Android奇巧既不如下工作描述的解决方案 - 你将需要寻找不同的方法,如如曼努埃尔Peinado的FadingActionBar 提供了一个滚动的标题为WebViews。

Update 2014-11-13: Since Android KitKat neither of the solutions described below are working -- you will need to look for different approaches like e.g. Manuel Peinado's FadingActionBar which provides a scrolling header for WebViews.

更新2012-07-08:延生的游戏好心创建的 TitleBarWebView 类带来预期的行为,回到到Android果冻豆。当旧的平台上使用它会使用隐藏的 setEmbeddedTitleBar()方法和果冻豆或以上使用时,它会模仿相同的行为。该人士$ ​​C $ C在Apache 2许可证在谷歌code

Update 2012-07-08: "Nobu games" kindly created a TitleBarWebView class bringing the expected behavior back to Android Jelly Bean. When used on older platforms it will use the hidden setEmbeddedTitleBar() method and when used on Jelly Bean or above it will mimic the same behavior. The source code is available under the Apache 2 license at google code

更新2012-06-30:看起来好像在 setEmbeddedTitleBar()方法已在安卓4.1又名果冻豆被删除: - (

Update 2012-06-30: It seems as if the setEmbeddedTitleBar() method has been removed in Android 4.1 aka Jelly Bean :-(

原来的答复:

它的是的可以放置一个的WebView 滚动型和它的工作。我在好消息的在Android 1.6设备上使用此。主要的缺点是用户不能滚动对角线,意思是:如果Web内容超过屏幕的宽度滚动型负责垂直滚动的的WebView 横向滚动。由于其中只有一个处理的触摸事件,你可以水平滚动的或的垂直而不是对角。

It is possible to place a WebView into a ScrollView and it does work. I am using this in GoodNews on Android 1.6 devices. The main drawback is that the user cannot scroll "diagonal" meaning: If the web content exceeds the width of the screen the ScrollView is responsible for vertical scrolling at the WebView for horizontal scrolling. As only one of them handles the touch events you can either scroll horizontally or vertically but not diagonal.

再往前还有由您(加载含量比previous的还要小的时候,例如空的垂直空间)中描述的一些恼人的问题。我找到的解决方法都掌握在好消息,但不记得他们了,因为我已经找到了一个更好的解决方案:

Further on there are some annoying problems as described by you (e.g. empty vertical space when loading a content smaller than the previous one). I've found workarounds for all of them in GoodNews, but cannot remember them now, because I've found a much better solution:

如果你只把的WebView 滚动型,可控制的上方的的网页内容,你确定只支持Android的2及以上的,那么你可以使用的WebView的隐藏的内部 setEmbeddedTitleBar()方法。它已经在API级别5被引入和(意外?)东窗事发了整整一个版本(我想应该是3.0)。

If you only put the WebView into the ScrollView to place Controls above the web content and you are OK to support only Android 2 and above, then you can use the hidden internal setEmbeddedTitleBar() method of the WebView. It has been introduced in API level 5 and (accidentally?) became public for exactly one release (I think it was 3.0).

此方法使您可以嵌入布局到的WebView 将被放在上面的网页内容。这种布局将滚动出垂直滚动,但将保持在当Web内容水平滚动同一水平位置时,屏幕上。

This method allows you to embed a layout into the WebView which will be placed above the web content. This layout will scroll out the screen when scrolling vertically but will be kept at the same horizontal position when the web content is scrolled horizontally.

由于该方法不为你所需要使用Java反射调用它的API出口。我建议派生一个新类如下:

As this method isn't exported by the API you need to use Java reflections to call it. I suggest to derive a new class as followed:

public final class WebViewWithTitle extends ExtendedWebView {
    private static final String LOG_TAG = "WebViewWithTitle";
    private Method setEmbeddedTitleBarMethod = null;

    public WebViewWithTitle(Context context) {
        super(context);
        init();
    }

    public WebViewWithTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        try {
            setEmbeddedTitleBarMethod = WebView.class.getMethod("setEmbeddedTitleBar", View.class);
        } catch (Exception ex) {
            Log.e(LOG_TAG, "could not find setEmbeddedTitleBar", ex);
        }
    }

    public void setTitle(View view) {
        if (setEmbeddedTitleBarMethod != null) {
            try {
                setEmbeddedTitleBarMethod.invoke(this, view);
            } catch (Exception ex) {
                Log.e(LOG_TAG, "failed to call setEmbeddedTitleBar", ex);
            }
        }
    }

    public void setTitle(int resId) {
        setTitle(inflate(getContext(), resId, null));
    }
}

然后在您的布局文件,你可以使用包括本

Then in your layout file you can include this using

<com.mycompany.widget.WebViewWithTitle
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

和其他人在你的$ C $某处c您可以拨打的setTitle()法与布局的ID嵌入到的WebView

and somewhere else in your code you can call the setTitle() method with the ID of the layout to be embedded into the WebView.

 
精彩推荐
图片推荐