【问题标题】:Passing id of adjacent view in xml to the custom view and retrieving it in constructor of custom view将xml中相邻视图的ID传递给自定义视图并在自定义视图的构造函数中检索它
【发布时间】:2012-12-08 15:02:29
【问题描述】:

下面是我的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();

    }

【问题讨论】:

  • 那么问题出在哪里?你已经有了属性数组。
  • 进度条的id是在/你的项目路径/gen/你的包名/R.java下构建项目时生成的。所以 id 将是 R.id.asyncLoadingProgress (这是一个 int 类型)。

标签: android android-layout android-widget android-custom-view


【解决方案1】:

现在我的问题是我想获取的资源标识符 AsyncImageView的构造函数中的进度条如下。

如果我没记错的话,您可以在构造函数中使用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"

【讨论】:

  • 现在你看到progressbar 和asyncimageview 是同一个父级。如何在AsyncImagView 的构造函数中获取progressbar 的View 对象?就像我们在活动 findViewbyId 的 OnCreate 中所做的那样,返回对视图对象的引用。
  • @Android_Crazy 你不应该在构造函数中这样做,因为你刚刚实例化了视图,而是覆盖AsyncImagView 类的onFinishInflate 方法,并在那里使用id属性以使用View progressBar = ((Activity)getContext()).findViewById(idFromAttribute); 获取目标视图。看看进展如何。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-22
  • 1970-01-01
  • 1970-01-01
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多