问题描述
我发现有很多类似的问题和答案,但我是不成功的修复。
我想一个ImageView的,这样一切我做的是在它的前面设置为背景。 (我不想使用xml背景选项,因为我会在活动范围内会改变我的背景,我无法弄清楚如何prevent从拉伸。)
谢谢!
BG =(ImageView的)findViewById(R.id.bg);
bg.setImageResource(R.drawable.marbles);
< ImageView的
机器人:ID =@ + ID / BG
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_centerVertical =真
机器人:SRC =@绘制/弹珠/>
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=稔。>
< ImageView的
机器人:ID =@ + ID / BG
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_centerVertical =真
机器人:SRC =@绘制/弹珠/> <的TextView
机器人:ID =@ + ID / tvTitle
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentTop =真
机器人:layout_centerHorizontal =真
机器人:文字=@字符串/ tvTitle
机器人:textAppearance =:/>中的Android ATTR / textAppearanceLarge? <的TextView
机器人:ID =@ + ID / tvInfo
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / tvTitle
机器人:layout_centerHorizontal =真
机器人:文字=@字符串/ tvInfo
机器人:textAppearance =机器人:ATTR / textAppearanceMedium/> <搜索栏
机器人:ID =@ + ID / sbMarbles
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_alignParentLeft =真
机器人:layout_alignRight =@ + ID / tvInfo/> <按钮
机器人:ID =@ + ID / butMarbles
风格=机器人:ATTR / buttonStyleSmall
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_alignParentRight =真
机器人:文字=@字符串/ butMarbles/> <的EditText
机器人:ID =@ + ID / tvMarbles
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_toLeftOf =@ + ID / butMarbles
机器人:layout_toRightOf =@ + ID / sbMarbles
机器人:EMS =10
安卓的inputType =号/> <的TextView
机器人:ID =@ + ID / tvInfo2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / tvInfo
机器人:layout_centerHorizontal =真
机器人:layout_marginTop =43dp
机器人:文字=@字符串/ tvInfo
机器人:textAppearance =机器人:ATTR / textAppearanceMedium/> <按钮
机器人:ID =@ + ID / butYes
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / tvInfo2
机器人:layout_marginTop =83dp
机器人:layout_toLeftOf =@ + ID / tvTitle
机器人:文字=@字符串/ butYes/> <按钮
机器人:ID =@ + ID / butNo
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBaseline =@ + ID / butYes
机器人:layout_alignBottom =@ + ID / butYes
机器人:layout_toRightOf =@ + ID / tvInfo2
机器人:文字=@字符串/ butNo/>< / RelativeLayout的>
LogCat中
21 01-07:00:00.453:E / AndroidRuntime(568):致命异常:主要
01-07 21:00:00.453:E / AndroidRuntime(568):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.evorlor.gameofnim / com.evorlor.gameofnim.Nim}:java.lang.ClassCastException:机器人.widget.TextView
01-07 21:00:00.453:E / AndroidRuntime(568):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-07 21:00:00.453:E / AndroidRuntime(568):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-07 21:00:00.453:E / AndroidRuntime(568):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
01-07 21:00:00.453:E / AndroidRuntime(568):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
01-07 21:00:00.453:E / AndroidRuntime(568):在android.os.Handler.dispatchMessage(Handler.java:99)
01-07 21:00:00.453:E / AndroidRuntime(568):在android.os.Looper.loop(Looper.java:123)
01-07 21:00:00.453:E / AndroidRuntime(568):在android.app.ActivityThread.main(ActivityThread.java:3683)
01-07 21:00:00.453:E / AndroidRuntime(568):在java.lang.reflect.Method.invokeNative(本机方法)
01-07 21:00:00.453:E / AndroidRuntime(568):在java.lang.reflect.Method.invoke(Method.java:507)
01-07 21:00:00.453:E / AndroidRuntime(568):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
01-07 21:00:00.453:E / AndroidRuntime(568):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-07 21:00:00.453:E / AndroidRuntime(568):在dalvik.system.NativeStart.main(本机方法)
01-07 21:00:00.453:E / AndroidRuntime(568):java.lang.ClassCastException:产生的原因android.widget.TextView
01-07 21:00:00.453:E / AndroidRuntime(568):在com.evorlor.gameofnim.Nim.listener(Nim.java:56)
01-07 21:00:00.453:E / AndroidRuntime(568):在com.evorlor.gameofnim.Nim.onCreate(Nim.java:41)
01-07 21:00:00.453:E / AndroidRuntime(568):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 21:00:00.453:E / AndroidRuntime(568):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-07 21:00:00.453:E / AndroidRuntime(568):11 ...更多
01-07 21:18:20.403:E / AndroidRuntime(704):致命异常:主要
01-07 21:18:20.403:E / AndroidRuntime(704):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.evorlor.gameofnim / com.evorlor.gameofnim.Opening}:显示java.lang.NullPointerException
01-07 21:18:20.403:E / AndroidRuntime(704):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-07 21:18:20.403:E / AndroidRuntime(704):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-07 21:18:20.403:E / AndroidRuntime(704):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
01-07 21:18:20.403:E / AndroidRuntime(704):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
01-07 21:18:20.403:E / AndroidRuntime(704):在android.os.Handler.dispatchMessage(Handler.java:99)
01-07 21:18:20.403:E / AndroidRuntime(704):在android.os.Looper.loop(Looper.java:123)
01-07 21:18:20.403:E / AndroidRuntime(704):在android.app.ActivityThread.main(ActivityThread.java:3683)
01-07 21:18:20.403:E / AndroidRuntime(704):在java.lang.reflect.Method.invokeNative(本机方法)
01-07 21:18:20.403:E / AndroidRuntime(704):在java.lang.reflect.Method.invoke(Method.java:507)
01-07 21:18:20.403:E / AndroidRuntime(704):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
01-07 21:18:20.403:E / AndroidRuntime(704):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-07 21:18:20.403:E / AndroidRuntime(704):在dalvik.system.NativeStart.main(本机方法)
01-07 21:18:20.403:E / AndroidRuntime(704):致:显示java.lang.NullPointerException
01-07 21:18:20.403:E / AndroidRuntime(704):在com.evorlor.gameofnim.Opening.listener(Opening.java:36)
01-07 21:18:20.403:E / AndroidRuntime(704):在com.evorlor.gameofnim.Opening.onCreate(Opening.java:28)
01-07 21:18:20.403:E / AndroidRuntime(704):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 21:18:20.403:E / AndroidRuntime(704):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-07 21:18:20.403:E / AndroidRuntime(704):11 ...更多
01-07 21:19:32.743:E / AndroidRuntime(737):致命异常:主要
01-07 21:19:32.743:E / AndroidRuntime(737):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.evorlor.gameofnim / com.evorlor.gameofnim.Opening}:显示java.lang.NullPointerException
01-07 21:19:32.743:E / AndroidRuntime(737):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-07 21:19:32.743:E / AndroidRuntime(737):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-07 21:19:32.743:E / AndroidRuntime(737):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
01-07 21:19:32.743:E / AndroidRuntime(737):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
01-07 21:19:32.743:E / AndroidRuntime(737):在android.os.Handler.dispatchMessage(Handler.java:99)
01-07 21:19:32.743:E / AndroidRuntime(737):在android.os.Looper.loop(Looper.java:123)
01-07 21:19:32.743:E / AndroidRuntime(737):在android.app.ActivityThread.main(ActivityThread.java:3683)
01-07 21:19:32.743:E / AndroidRuntime(737):在java.lang.reflect.Method.invokeNative(本机方法)
01-07 21:19:32.743:E / AndroidRuntime(737):在java.lang.reflect.Method.invoke(Method.java:507)
01-07 21:19:32.743:E / AndroidRuntime(737):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
01-07 21:19:32.743:E / AndroidRuntime(737):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-07 21:19:32.743:E / AndroidRuntime(737):在dalvik.system.NativeStart.main(本机方法)
01-07 21:19:32.743:E / AndroidRuntime(737):致:显示java.lang.NullPointerException
01-07 21:19:32.743:E / AndroidRuntime(737):在com.evorlor.gameofnim.Opening.listener(Opening.java:36)
01-07 21:19:32.743:E / AndroidRuntime(737):在com.evorlor.gameofnim.Opening.onCreate(Opening.java:28)
01-07 21:19:32.743:E / AndroidRuntime(737):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 21:19:32.743:E / AndroidRuntime(737):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-07 21:19:32.743:E / AndroidRuntime(737):11 ...更多
使用 RelativeLayout的
或的FrameLayout
和定义你的的ImageView
作为第一个孩子。限定的第一对象是在z顺序最低(即,它们将下方的其它视图)。
I have found many similar questions and answers, but I was unsuccessful with those fixes.
I want to set an ImageView to the background so that everything else I am doing is in front of it. (I do not want to use the xml background option because I will be changing my background within the activity and I cannot figure out how to prevent it from stretching.)
Thanks!
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
Use a RelativeLayout
or a FrameLayout
and define your ImageView
as the first child. The first objects defined are the lowest in the z-order (i.e. they will be "below" the other views).
这篇关于一个的ImageView设置为背景的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!