首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我使用来自attrs.xml的属性时,布局中的错误

当我使用来自attrs.xml的属性时,布局中的错误
EN

Stack Overflow用户
提问于 2016-07-29 09:10:22
回答 1查看 59关注 0票数 0

我试图使用attrs.xml中的两个属性,如:contenthandle。当我在构建布局时使用它们时,视图就会消失。我已经尝试了三天的错误,但没有任何效果。帮助是非常感谢的!

上下文中的代码

代码语言:javascript
复制
<hh.hhh.app.android.hhhh.widget.ExpandablePanel
    android:id="@+id/expandablePanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:handle="@+id/expandButton"
    app:content="@+id/listViewProduct"
    app:collapsedHeight="50dip"
    app:animationDuration="25">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listViewProduct"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/expandButton"/>
</hh.hhh.app.android.hhhh.widget.ExpandablePanel>

attrs.xml代码

代码语言:javascript
复制
    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ExpandablePanel">
        <attr name="handle" format="reference" />
        <attr name="content" format="reference" />
        <attr name="collapsedHeight" format="dimension"/>
        <attr name="animationDuration" format="integer"/>
    </declare-styleable>
</resources>
EN

回答 1

Stack Overflow用户

发布于 2016-07-29 09:54:05

我以我自己的自定义类为例。因此,对于所有自定义函数,您需要首先检查它是否在EditMode中。isInEditMode()告诉我们,布局是通过IDE预览或电话呈现的。IDE预览功能不足以运行我们的自定义代码,因此建议不要在预览呈现时运行我们的自定义。我正在附加我的自定义类,以了解确切的场景。

代码语言:javascript
复制
    public class DynamicImageView extends ImageView {
    static DynamicImageTypeListener masterImageTypeListener;
    DynamicImageTypeListener imageTypeListener;
    int imageResourceArray;

    public DynamicImageView(Context context, AttributeSet attr, int defStyle) {
        super(context, attr, defStyle);
        initValues(attr, defStyle);
    }

    public DynamicImageView(Context context, AttributeSet attr) {
        super(context, attr);
        initValues(attr, 0);
    }

    public DynamicImageView(Context context) {
        super(context);
    }

    public static void setMasterImageTypeListener(DynamicImageTypeListener masterImageTypeListener) {
        DynamicImageView.masterImageTypeListener = masterImageTypeListener;
    }

    public void setImageTypeListener(DynamicImageTypeListener imageTypeListener) {
        this.imageTypeListener = imageTypeListener;
    }

    private void initValues(AttributeSet attr, int defStyle) {
        if (!isInEditMode()) {  //This code will run only on phone not by preview.
            TypedArray a = getContext().obtainStyledAttributes(attr, R.styleable.DynamicImageView, defStyle, 0);
            imageResourceArray = a.getResourceId(0, 0);
            updateImage();
            a.recycle();
        }
    }

    public void updateImage() {
        TypedArray imgs = getResources().obtainTypedArray(imageResourceArray);
        int imageIndex = imageTypeListener != null ? imageTypeListener.getImageType(getContext()) : (masterImageTypeListener != null ? masterImageTypeListener.getImageType(getContext()) : 0);
        setImageResource(imgs.getResourceId(imageIndex, -1));
    }
}

希望能有所帮助:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38654874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档