我想根据ImageButton
目录中的图像动态地将 s 添加到布局中,但我希望它们都具有与 xml 文件中指定的格式相同的格式。
我试过这个
XmlPullParser parser = res.getXml(R.layout.imagebuttons);
AttributeSet attributes = Xml.asAttributeSet(parser);
setLayoutParams(new LayoutParams(context, attributes));
阅读此 xml
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="90"
android:layout_height="160"
android:scaleType="fitCenter"
android:onClick="zoomImageFromThumb" >
</ImageButton>
但我收到了这个错误
E/AndroidRuntime(16602): java.lang.RuntimeException: Unable to start activity ComponentInfo{package/package.MainActivity}: java.lang.RuntimeException:
Binary XML file line #-1: You must supply a layout_width attribute.
E/AndroidRuntime(16602): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
E/AndroidRuntime(16602): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
E/AndroidRuntime(16602): at android.app.ActivityThread.access$600(ActivityThread.java:130)
E/AndroidRuntime(16602): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
E/AndroidRuntime(16602): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(16602): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(16602): at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime(16602): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(16602): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(16602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime(16602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(16602): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(16602): Caused by: java.lang.RuntimeException: Binary XML file line #-1: You must supply a layout_width attribute.
E/AndroidRuntime(16602): at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
E/AndroidRuntime(16602): at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5458)
E/AndroidRuntime(16602): at android.view.ViewGroup$LayoutParams.<init>(ViewGroup.java:5411)
E/AndroidRuntime(16602): at package.ImageButtons.<init>(ImageButtons.java:27)
E/AndroidRuntime(16602): at package.MainActivity.onCreate(MainActivity.java:49)
E/AndroidRuntime(16602): at android.app.Activity.performCreate(Activity.java:5122)
E/AndroidRuntime(16602): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1089)
E/AndroidRuntime(16602): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
E/AndroidRuntime(16602): ... 11 more
我不知道如何解决这个问题,有什么我很明显的遗漏吗?