我想動態地將ImageButton
s添加到基於目錄中圖像的佈局,但我希望它們都具有與xml文件中指定的格式相同的格式。將屬性從xml添加到動態添加的ImageButton
我已經試過這
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
我迷路了,至於如何解決這個問題,有什麼明顯的我失蹤了嗎?
加了他們,還是一樣的錯誤 – Cob50nm