在imageview上设置可滚动的webview?

时间:2013-09-19 03:45:45

标签: android android-layout imageview scrollview transparency

我有一个透明的图像视图,我想将其设置为我的应用程序的背景。如果我将其作为android:background="@drawable/awesome50"进行拉伸图像,则圆圈变为椭圆形,并且会失去纵横比。这就是为什么我想把我的webview放在imageview上。所以我写了这个xml代码,但是当应用程序运行时崩溃了。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dp"
android:id="@+id/jokeslayout"
 >

 <ImageView
    android:id="@+id/ivAwesome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/awesome50"
    />

<ScrollView
android:id="@+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">

<WebView
    android:id="@+id/tvAnimalJoke"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge"

     />

</ScrollView>

这是LogCat

    09-19 03:37:01.853: D/dalvikvm(2988): GC_FOR_ALLOC freed <1K, 1% free 19526K/19684K, paused 48ms, total 48ms
09-19 03:37:02.213: I/dalvikvm-heap(2988): Grow heap (frag case) to 46.314MB for 28451572-byte allocation
09-19 03:37:02.483: D/dalvikvm(2988): GC_CONCURRENT freed 0K, 1% free 47310K/47472K, paused 75ms+66ms, total 266ms
09-19 03:37:03.913: D/AndroidRuntime(2988): Shutting down VM
09-19 03:37:03.962: W/dalvikvm(2988): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-19 03:37:04.043: E/AndroidRuntime(2988): FATAL EXCEPTION: main
09-19 03:37:04.043: E/AndroidRuntime(2988): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mkaya.ultimatejokescollection/com.mkaya.ultimatejokescollection.AnimalJokes}: java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.webkit.WebView
09-19 03:37:04.043: E/AndroidRuntime(2988):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-19 03:37:04.043: E/AndroidRuntime(2988):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-19 03:37:04.043: E/AndroidRuntime(2988):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-19 03:37:04.043: E/AndroidRuntime(2988):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-19 03:37:04.043: E/AndroidRuntime(2988):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 03:37:04.043: E/AndroidRuntime(2988):     at android.os.Looper.loop(Looper.java:137)
09-19 03:37:04.043: E/AndroidRuntime(2988):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-19 03:37:04.043: E/AndroidRuntime(2988):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 03:37:04.043: E/AndroidRuntime(2988):     at java.lang.reflect.Method.invoke(Method.java:511)
09-19 03:37:04.043: E/AndroidRuntime(2988):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-19 03:37:04.043: E/AndroidRuntime(2988):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-19 03:37:04.043: E/AndroidRuntime(2988):     at dalvik.system.NativeStart.main(Native Method)
09-19 03:37:04.043: E/AndroidRuntime(2988): Caused by: java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.webkit.WebView
09-19 03:37:04.043: E/AndroidRuntime(2988):     at com.mkaya.ultimatejokescollection.AnimalJokes.onCreate(AnimalJokes.java:72)
09-19 03:37:04.043: E/AndroidRuntime(2988):     at android.app.Activity.performCreate(Activity.java:5104)
09-19 03:37:04.043: E/AndroidRuntime(2988):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-19 03:37:04.043: E/AndroidRuntime(2988):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-19 03:37:04.043: E/AndroidRuntime(2988):     ... 11 more

现在,如果我按如下方式更改我的xml,这次应用程序不会崩溃,但由于透明的imageview位于文本的顶部,因此文本也变得透明。我希望文本保持不透明。我们得到了不受欢迎的效果,如下面的屏幕截图所示。

xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dp"
android:id="@+id/jokeslayout"
 >



<ScrollView
android:id="@+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">

<WebView
    android:id="@+id/tvAnimalJoke"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge"

     />

</ScrollView>

    <ImageView
    android:id="@+id/ivAwesome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/awesome50"
    />

及其不良结果(与图像重合的文字变得透明)

http://i.stack.imgur.com/5RpwM.png

2 个答案:

答案 0 :(得分:0)

使用frameLayout而不是相对布局尝试一次,您可以相应地设计您要查找的内容。此链接可以为您提供帮助 - Placing/Overlapping(z-index) a view above another view in androidhttp://developer.android.com/reference/android/widget/FrameLayout.html

答案 1 :(得分:0)

尝试这个... webview已经滚动了所以不需要将它放入scrollview

<RelativeLayout
android:id="@+id/jokeslayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dp" >

<ImageView
    android:id="@+id/ivAwesome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/splashscreen" />

<WebView
    android:id="@+id/tvAnimalJoke"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>