gpt4 book ai didi

android - 将 ImageView 设置为后台 Android

转载 作者:太空狗 更新时间:2023-10-29 15:47:36 25 4
gpt4 key购买 nike

我找到了许多类似的问题和答案,但我没有成功修复这些问题。

我想将一个 ImageView 设置为背景,这样我正在做的所有其他事情都在它的前面。 (我不想使用 xml 背景选项,因为我将在 Activity 中更改我的背景,我不知道如何防止它拉伸(stretch)。)

谢谢!

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>

日志猫

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

最佳答案

使用 RelativeLayoutFrameLayout 并将您的 ImageView 定义为第一个 child 。定义的第一个对象在 z 顺序中最低(即它们将“低于”其他 View )。

关于android - 将 ImageView 设置为后台 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14207230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com