アプリの背景として設定したい透明なイメージビューがあります。android:background="@drawable/awesome50"
画像を引き伸ばすようにすると、円が楕円形になり、アスペクト比が失われます。だからこそ、私のウェブビューをイメージビューの上に置きたいのです。だから私はこの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"
/>
そしてその望ましくない結果(画像と一致するテキストが透明になります)