将属性从xml添加到动态添加的ImageButton

时间:2013-08-27 13:41:51

标签: android android-layout android-imageview android-xml

我想动态地将ImageButton添加到目录中图像的布局基础上,但我希望它们具有与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

我迷失了如何解决这个问题,是否有一些我很遗憾的事情?

2 个答案:

答案 0 :(得分:0)

您忘记在xml文件的宽度和高度属性中提及“dp”:

<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="90dp"
    android:layout_height="160dp" 
    android:scaleType="fitCenter"
    android:onClick="zoomImageFromThumb" >
</ImageButton>

答案 1 :(得分:0)

这非常简单,我实际上并没有使用解析器,我认为AttributesSet就是这样做的。这是我使用I cannot read the AttributeSet from my XML resources

的答案