我正在尝试以编程方式实现一些ImageView
按钮,因此我可以在运行时对它们进行排序。
我使用名为image_bg
的布局资源作为XML按钮的背景,如activity_main.xml
中所示:
<ImageView android:layout_margin="1dp"
android:clickable="true"
android:layout_weight="1"
android:layout_width="0dp"
android:id="@+id/imageButton"
android:src="@drawable/a"
android:background="@layout/image_bg"
android:scaleType="fitCenter"/>
尝试以编程方式将背景设置为layout / image_bg,我遇到了这个错误:
无论我使用什么组合命令,我都无法设置布局文件的背景。我进行了广泛的搜索,并尝试了getResources().getDrawable(R.layout.image_bg)
的各种组合,并使用了setBackground(R.layout.image_bg)
- 但到目前为止我没有运气。
编辑:--------------------------------------------- ---
按照建议将image_bg.xml文件移动到R.drawable后,修复了语法错误,但在运行时发生了以下错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{m.fugl/m.fugl.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f02006a
image_bg.xml的内容:
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@android:color/transparent"
android:layout_height="match_parent"
android:layout_width="match_parent">
<solid android:color="#000000" />
<stroke android:width="1dp" android:color="#776da8"/>
<corners android:bottomRightRadius="3dp" android:bottomLeftRadius="3dp" android:topLeftRadius="3dp" android:topRightRadius="3dp" />
<padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp" />
</shape>
我已经尝试过运行gradlew clean,但它没有帮助。
EDIT2 ---------------------------------------------- -----:
尝试将我的image_bg.xml文件更改为:
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#000000" />
<stroke android:width="1dp" android:color="#776da8"/>
<corners android:bottomRightRadius="3dp" android:bottomLeftRadius="3dp" android:topLeftRadius="3dp" android:topRightRadius="3dp" />
<padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp" />
</shape>
我仍然收到错误。整个堆栈跟踪:
07-01 18:14:58.817 977-977/m.fugl E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: m.fugl, PID: 977
java.lang.RuntimeException: Unable to start activity ComponentInfo{m.fugl/m.fugl.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f02006a
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access$900(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f02006a
at android.content.res.Resources.getValue(Resources.java:1310)
at android.content.res.Resources.getDrawable(Resources.java:790)
at android.content.res.Resources.getDrawable(Resources.java:770)
at android.content.Context.getDrawable(Context.java:402)
at android.view.View.setBackgroundResource(View.java:16105)
at m.fugl.SoundButton.<init>(SoundButton.java:58)
at m.fugl.MainActivity.onCreate(MainActivity.java:111)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access$900(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
EDIT3 ---------------------------------:
Soundbutton是一个包含Imageview,声音和用于播放声音的各种方法的类。这是它的构造函数:
SoundButton(int uri, Context context, int src, CharSequence text) {
//Binder lyd og knap til constructor parametre
globalUri = uri;
globalContext = context;
globalText = text;
buttonId = new ImageView(context);
createSoundForButton();
buttonId.setImageResource(src);
buttonId.setBackgroundResource(R.drawable.image_bg);
buttonId.setClickable(true);
buttonId.setHapticFeedbackEnabled(true);
buttonId.setLongClickable(false);
buttonId.setHovered(false);
setButtonIsPlaying(false);
setVisible(false);
// Når der trykkes på knappen....
buttonId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
playOrPauseSound();
}
});
buttonId.setLongClickable(true);
buttonId.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
PopupMenu popupMenu = new PopupMenu(globalContext, view);
popupMenu.setOnMenuItemClickListener(SoundButton.this);
popupMenu.inflate(R.menu.popup_menu);
popupMenu.show();
buttonId.setBackgroundColor(Color.BLUE);
popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
@Override
public void onDismiss(PopupMenu menu) {
setButtonIsPlaying(false);
}
});
return true;
}
});
}
问题应该在58号线上: buttonId.setBackgroundResource(R.drawable.image_bg);
EDIT4 ---------------------------------: 我找到了答案。我将image_bg.xml文件从Android Studio内部复制到drawables文件夹,这使它落在drawable-large-mdpi文件夹中,而不是drawable文件夹。将image_bg.xml复制到drawable文件夹后,它现在可以工作了,因为编译器可以找到xml文件!
答案 0 :(得分:2)
查看您发布的图片,您正在提供mContext
的值,而正在等待来自R.layout
的int。
R.drawable
应该是R.layout.image_bg
,希望您将R.drawable.image_bg
放在其中一个可绘制文件夹中
答案 1 :(得分:0)
将图片放入可绘制文件夹
myImgView.setBackgroundResource(R.drawable.image_bg);
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.image_bg));
答案 2 :(得分:0)
您无法将布局设置为背景
您需要将图像放在drawable文件夹中,然后使用getResources().getDrawable(R.layout.image_bg)
然后
setBackground(R.layout.image_bg)