自定义带有XML子项的Android组件



我是一名iOS开发人员,对Android相对较新,我正在尝试创建可重复使用的组件,以分离控制器逻辑和视图定义。我想要一个类似于iOS IBOutlets的模式,在那里你定义了一个可以与不同xib文件或故事板布局一起使用的类。

例如,假设我想创建一个自定义进度条组件。我希望用户能够提供所需的子项,并在xml中分别设计和设置它们的样式。

以下是我试图实现的一些伪代码:

layout.xml

<FrameLayout>
    <!-- A vertical progress bar -->
    <CustomProgressBar>
        <LinearLayout orientation="vertical">
            <ImageView id="@+id/bar" src="@drawable/bar_image" />
            <TextView id="@+id/label" text="Bar 1"/>
        </LinearLayout>
    </CustomProgressBar>
    <!-- A horizontal bar using same controller class -->
    <CustomProgressBar>
        <LinearLayout orientation="horizontal">
            <ImageView src="@drawable/background_image" />
            <ImageView id="@+id/bar" src="@drawable/bar_image" />
            <TextView id="@+id/label" text="Bar 1"/>
        </LinearLayout>
    </CustomProgressBar>
<FrameLayout>

那么我的自定义类可能看起来像:

public class CustomProgressBar extends FrameLayout {
    private ImageView bar;
    private TextView label;
    .
    .
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        // Store the references of the components
        bar = (ImageView) findViewById(R.id.bar);
        label = (TextView) findViewById(R.id.label);
        // Now I should be able to write general code for this component
        // using the reference components I found
    }
}

在上面的示例中,开发人员正在同一个xml文件中实例化2个CustomProgressBars。但是每个栏的布局有很大的不同(子显示树和方向不同)。这里明显的问题是xml不会编译,因为我对xml中的不同视图使用相同的id。为了解决编译问题,我可以更改id名称,但控制器类将不知道如何查找对这些子级的引用。

有没有其他方法可以解决这个问题?

ViewGroup中有一些方法称为getChildCountgetChildAt,它们允许您按索引提取子视图。

所以你要做的是:

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    getProgressViews(this);
}
private ImageView bar;
private TextView label;
private void getProgressViews(ViewGroup viewGroup) {
    int childCount = viewGroup.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View view = viewGroup.getChildAt(i);
        if (view.getClass().getSuperclass() == ViewGroup.class) {
            getProgressViews((ViewGroup) view);
        }
        if (view instanceof ImageView) {
            bar = (ImageView) view;
        }
        if (view instanceof TextView) {
            label = (TextView) view;
        }
    }
}

如果您希望使用这些来手动将它们绘制到屏幕上,则需要覆盖onLayout方法。

最新更新