我有一个透明的图像视图,我想将其设置为我的应用程序的背景。如果我在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,这一次应用程序不会崩溃,但由于透明图像视图位于文本之上,因此文本也变得透明。我希望文本保持不透明。我们得到了如下图所示的不良效果。
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"
/>
及其不良结果(与图像重合的文本变为透明)