0

我想根据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

我不知道如何解决这个问题,有什么我很明显的遗漏吗?

4

2 回答 2

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>
于 2013-08-27T14:00:25.910 回答
0

这是非常简单的事情,我实际上并没有使用解析器,我认为 AttributesSet 做到了。这是我使用的答案我无法从我的 XML 资源中读取 AttributeSet

于 2013-08-27T15:05:43.853 回答