【问题标题】:WebView doesn't align top or bottom in its containerWebView 未在其容器中对齐顶部或底部
【发布时间】:2013-07-14 00:10:47
【问题描述】:

我有一个奇怪的问题。

我有一个 ImageView,在这个 ImageView 上是一个 WebView。这个 WebView 和 ImageView 一样大,而且它的背景是透明的,所以 WebView 只显示文本。

在我的 Galaxy Nexus 上,这非常有效。 但是在我的 Sony Xperia 上,WebView 没有对齐图像的顶部和底部。左对齐和右对齐都可以。

但是为什么会这样呢?

这是我的 xml 代码:

 <ImageView
    android:id="@+id/ImageView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:src="@drawable/product_pic_background" />

<RelativeLayout
    android:id="@+id/contain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignBottom="@+id/ImageView02"
    android:layout_alignLeft="@+id/ImageView02"
    android:layout_alignRight="@+id/ImageView02"
    android:layout_alignTop="@+id/ImageView02"
    android:orientation="vertical"
    android:paddingBottom="15dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:paddingTop="50dp" >

    <WebView
        android:id="@+id/produkttext"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_alignParentStart="true"           
        android:longClickable="false"
        android:scrollbars="vertical" />
</RelativeLayout>

1.Xperia 弧线。它不应该是怎样的 2.银河连结。应该怎样

【问题讨论】:

  • 使用dip而不是dp,看看它是否会给你不同的结果:)
  • 能否请您粘贴两个设备的屏幕截图并在此处粘贴这些设备的确切型号。谢谢
  • 添加两张图片...
  • 如果我理解正确,请将android:layout_height="match_parent" 设为ImageView。如果失败了,我怀疑,然后在RelativeLayout 中包含ImageView 可能吗?
  • 从您所做的最后一次编辑中:“1.Xperia arc。它不应该是 2.Galaxy Nexus。它应该是什么”-应该是哪个-我猜是第一个。如果您希望文本被图像“框架”“限制”(封闭),那么您应该将它们都添加到RelativeLayout

标签: android webview alignment


【解决方案1】:

好的,一个解决方案,一旦我们有要求:

<RelativeLayout
    android:id="@+id/contain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="15dip"
    android:paddingLeft="15dip"
    android:paddingRight="15dip"
    android:paddingTop="50dip" >

    <WebView
        android:id="@+id/produkttext"
        android:longClickable="false"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:background="@+drawable/product_pic_background"
        android:scrollbars="vertical" />
</RelativeLayout>

然后在代码中:

WebView web = (WebView) findViewById(R.id.produkttext);
web.setBackgroundColor(0);
BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(R.drawable.product_pic_background);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
rl.getLayoutParams().height = bd.getBitmap().getHeight();
rl.getLayoutParams().width = bd.getBitmap().getWidth();

这将使WebView 透明,以便查看背景图像,以及通过宽度和高度匹配父布局。 然后,RelativeLayout 将设置图片可绘制对象的尺寸。 我还没有设法测试它(我现在不能),但如果这不起作用,我将删除此评论(并且可能会提交 harakiri :-))。

【讨论】:

  • 你的想法很聪明。但现在它随着整个图片缩放
  • 这不是想法吗?“框架”与图片一样大,文本可在其中滚动?
  • webview 应该和图片一样大。
  • 根据你的想法,text-webview 仍然像第一张图片一样缩放
  • 那个叠加很难获得,但是你可以在程序中获取图片的大小并设置WebView的尺寸。我稍后会用正确的结果编辑我的回复。
猜你喜欢
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
  • 1970-01-01
  • 2017-06-25
相关资源
最近更新 更多