将ImageView设置为Android背景

时间:2013-01-08 02:11:52

标签: android eclipse background imageview

我发现了许多类似的问题和答案,但我没有成功解决这些问题。

我想将ImageView设置为背景,以便我正在做的其他事情就在它的前面。 (我不想使用xml背景选项,因为我将在活动中更改我的背景,我无法弄清楚如何防止它拉伸。)

谢谢!

bg = (ImageView) findViewById(R.id.bg);
bg.setImageResource(R.drawable.marbles);

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Nim" >


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

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/tvTitle"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tvInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvTitle"
        android:layout_centerHorizontal="true"
        android:text="@string/tvInfo"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <SeekBar
        android:id="@+id/sbMarbles"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/tvInfo" />

    <Button
        android:id="@+id/butMarbles"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/butMarbles" />

    <EditText
        android:id="@+id/tvMarbles"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/butMarbles"
        android:layout_toRightOf="@+id/sbMarbles"
        android:ems="10"
        android:inputType="number" />

    <TextView
        android:id="@+id/tvInfo2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvInfo"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="43dp"
        android:text="@string/tvInfo"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/butYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvInfo2"
        android:layout_marginTop="83dp"
        android:layout_toLeftOf="@+id/tvTitle"
        android:text="@string/butYes" />

    <Button
        android:id="@+id/butNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/butYes"
        android:layout_alignBottom="@+id/butYes"
        android:layout_toRightOf="@+id/tvInfo2"
        android:text="@string/butNo" />

</RelativeLayout>

logcat的

01-07 21:00:00.453: E/AndroidRuntime(568): FATAL EXCEPTION: main
01-07 21:00:00.453: E/AndroidRuntime(568): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evorlor.gameofnim/com.evorlor.gameofnim.Nim}: java.lang.ClassCastException: android.widget.TextView
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.os.Looper.loop(Looper.java:123)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-07 21:00:00.453: E/AndroidRuntime(568):  at java.lang.reflect.Method.invokeNative(Native Method)
01-07 21:00:00.453: E/AndroidRuntime(568):  at java.lang.reflect.Method.invoke(Method.java:507)
01-07 21:00:00.453: E/AndroidRuntime(568):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-07 21:00:00.453: E/AndroidRuntime(568):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-07 21:00:00.453: E/AndroidRuntime(568):  at dalvik.system.NativeStart.main(Native Method)
01-07 21:00:00.453: E/AndroidRuntime(568): Caused by: java.lang.ClassCastException: android.widget.TextView
01-07 21:00:00.453: E/AndroidRuntime(568):  at com.evorlor.gameofnim.Nim.listener(Nim.java:56)
01-07 21:00:00.453: E/AndroidRuntime(568):  at com.evorlor.gameofnim.Nim.onCreate(Nim.java:41)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-07 21:00:00.453: E/AndroidRuntime(568):  ... 11 more
01-07 21:18:20.403: E/AndroidRuntime(704): FATAL EXCEPTION: main
01-07 21:18:20.403: E/AndroidRuntime(704): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evorlor.gameofnim/com.evorlor.gameofnim.Opening}: java.lang.NullPointerException
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.os.Looper.loop(Looper.java:123)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-07 21:18:20.403: E/AndroidRuntime(704):  at java.lang.reflect.Method.invokeNative(Native Method)
01-07 21:18:20.403: E/AndroidRuntime(704):  at java.lang.reflect.Method.invoke(Method.java:507)
01-07 21:18:20.403: E/AndroidRuntime(704):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-07 21:18:20.403: E/AndroidRuntime(704):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-07 21:18:20.403: E/AndroidRuntime(704):  at dalvik.system.NativeStart.main(Native Method)
01-07 21:18:20.403: E/AndroidRuntime(704): Caused by: java.lang.NullPointerException
01-07 21:18:20.403: E/AndroidRuntime(704):  at com.evorlor.gameofnim.Opening.listener(Opening.java:36)
01-07 21:18:20.403: E/AndroidRuntime(704):  at com.evorlor.gameofnim.Opening.onCreate(Opening.java:28)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-07 21:18:20.403: E/AndroidRuntime(704):  ... 11 more
01-07 21:19:32.743: E/AndroidRuntime(737): FATAL EXCEPTION: main
01-07 21:19:32.743: E/AndroidRuntime(737): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evorlor.gameofnim/com.evorlor.gameofnim.Opening}: java.lang.NullPointerException
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.os.Looper.loop(Looper.java:123)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-07 21:19:32.743: E/AndroidRuntime(737):  at java.lang.reflect.Method.invokeNative(Native Method)
01-07 21:19:32.743: E/AndroidRuntime(737):  at java.lang.reflect.Method.invoke(Method.java:507)
01-07 21:19:32.743: E/AndroidRuntime(737):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-07 21:19:32.743: E/AndroidRuntime(737):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-07 21:19:32.743: E/AndroidRuntime(737):  at dalvik.system.NativeStart.main(Native Method)
01-07 21:19:32.743: E/AndroidRuntime(737): Caused by: java.lang.NullPointerException
01-07 21:19:32.743: E/AndroidRuntime(737):  at com.evorlor.gameofnim.Opening.listener(Opening.java:36)
01-07 21:19:32.743: E/AndroidRuntime(737):  at com.evorlor.gameofnim.Opening.onCreate(Opening.java:28)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-07 21:19:32.743: E/AndroidRuntime(737):  ... 11 more

1 个答案:

答案 0 :(得分:9)

使用RelativeLayoutFrameLayout并将ImageView定义为第一个孩子。定义的第一个对象是z顺序中的最低对象(即它们将“低于”其他视图)。