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

Ash*_*ali 3 android android-widget android-custom-view android-layout

下面是我的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>
Run Code Online (Sandbox Code Playgroud)

你可以看到

应用程式: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>
Run Code Online (Sandbox Code Playgroud)

现在我的问题是我想在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();

    }
Run Code Online (Sandbox Code Playgroud)

Luk*_*rog 8

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

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

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AsyncImageView, defStyle, 0);
int progressId = a.getResourceId(R.styleable.AsyncImageView_progressId, 0);
Run Code Online (Sandbox Code Playgroud)

另外,如果你声明的标识ProgressBarAsyncImageView属性,那么我想你需要设置的ID喜欢本作的ProgressBar:

android:id="@id/asyncLoadingProgress"
Run Code Online (Sandbox Code Playgroud)

  • @Android_Crazy您不应该在构造函数中执行此操作,因为您刚刚实例化了视图,而是重写“AsyncImagView”类的“onFinishInflate”方法,并在其中使用属性中的“id”来获取目标视图“查看进度条 = ((Activity)getContext()).findViewById(idFromAttribute);`。看看进展如何。 (2认同)