android– 将xml中相邻视图的id传递给自定义视图,并在自定义视图的构造函数中检索它

android– 将xml中相邻视图的id传递给自定义视图,并在自定义视图的构造函数中检索它,第1张

概述下面是我的xml<RelativeLayoutxmlns:android="http://schemas.android.com/apkes/android"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="5dp"xmlns:app="http

下面是我的xml

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:padding="5dp" xmlns:app="http://schemas.androID.com/apk/res/com.infibeam.allthingsd.apps.spinr">   <com.asyncimageWidget.AsyncImageVIEw        androID:ID="@+ID/discover_List_icon"        androID:layout_wIDth="130dp"        androID:layout_height="130dp"        androID:layout_alignParentleft="true"        androID:layout_centerHorizontal="true"        app:progressID="@+ID/asyncLoadingProgress"         />    <Progressbar        androID:ID="@+ID/asyncLoadingProgress"        androID:layout_wIDth="130dp"        androID:layout_height="130dp"        androID:layout_alignParentleft="true"        androID:layout_centerHorizontal="true"     /></relativeLayout>

你可以看到

app:progressID=”@+ID/asyncLoadingProgress”

这是我在attrs.xml中定义的自定义属性,如下所示.

<resources>    <declare-styleable name="AsyncImageVIEw">        <attr name="defaultSrc" format="reference" />        <attr name="parentID" format="reference" />        <attr name="progressID" format="reference" />        <attr name="url" format="string" />        <attr name="inDensity">            <enum name="ldpi" value="120" />            <enum name="mdpi" value="160" />            <enum name="hdpi" value="240" />            <enum name="xhdpi" value="320" />        </attr>    </declare-styleable></resources>

现在我的问题是我想在AsyncImageVIEw的构造函数中获取Progressbar的资源标识符,如下所示.

public AsyncImageVIEw(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        initializeDefaultValues();        TypedArray a = context.obtainStyledAttributes(attrs,                R.styleable.AsyncImageVIEw, defStyle, 0);        Drawable d = a.getDrawable(R.styleable.AsyncImageVIEw_defaultSrc);        if (d != null) {            setDefaultimageDrawable(d);        }        final int inDensity = a                .getInt(R.styleable.AsyncImageVIEw_inDensity, -1);        if (inDensity != -1) {            setInDensity(inDensity);        }        setUrl(a.getString(R.styleable.AsyncImageVIEw_url));        a.recycle();    }

解决方法:

Now My question is I want to obtain the resource IDentifIEr of
Progressbar in constructor of AsyncImageVIEw which is as follows.

如果我没弄错,你可以在构造函数中使用getResourceId()方法:

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AsyncImageVIEw, defStyle, 0);int progressID = a.getResourceID(R.styleable.AsyncImageVIEw_progressID, 0);

另外,如果在AsyncImageVIEw属性中声明Progressbar的ID,那么我认为你需要为Progressbar设置这样的ID:

androID:ID="@ID/asyncLoadingProgress"

总结

以上是内存溢出为你收集整理的android – 将xml中相邻视图的id传递给自定义视图,并在自定义视图的构造函数中检索它全部内容,希望文章能够帮你解决android – 将xml中相邻视图的id传递给自定义视图,并在自定义视图的构造函数中检索它所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1114429.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存