使用一组现有小部件创建自定义小部件?

Creating a custom widget with a group of existing widgets?

我已经四处寻找如何做到这一点,但我仍然不是很确定,所以我决定在这里问一下。

我想要创建一个小部件列表,这些小部件将显示艺术家姓名、流派和他们将要演奏的场地。下面是 4 个小部件的图片,由一个 imageView 和 3 个纯文本视图组成,我想以某种方式分组并创建 1 个自定义小部件,我可以调用所有 4 个组件来显示相关信息。

我还希望让这个小部件在设定的限制范围内自我复制,显示不同的艺术家、流派和地点。

我完全不知道如何解决这个问题,请提供任何帮助! 提前致谢!

图片:

http://puu.sh/hr9x4/b3bffdf263.jpg

编辑:撞!任何帮助将不胜感激!

创建自定义视图。

创建一个 class 小部件并使用线性布局扩展它。在构造函数中膨胀包含您的布局(图像视图和 3 个文本视图)的布局。

通过调用 addView

将膨胀视图添加到 class 本身

编写 setter 函数,您将在其中接受事件对象 class,其中包含与事件相关的信息,并且该函数只需将值设置为适当的文本视图。

Class Widget extends LinearLayout{
private TextView artistName;
public Widget(Context context) {
        super(context);

        View view =  LayoutInflater.from(getContext()).inflate(
                R.layout.widget, null);
        artistName = view.findViewById(R.id.artistName);
        this.addView(view);

    }

public void setName(Event event){
   artistName.setText(event.getArtistName());
}
}

在您的 activity 布局中,您可以将其用作

<pkg_name.Widget android:id="@+id/widget1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</pkg_name.Widget>

坦率地说,你应该从读一篇official documentation for Android developers because there you can find the basics for the asked question. If you’d like to learn examples and see the code in details, take a look at the my experience of creating a custom Android widget开始。在那里我不得不解决小部件透明度的问题。也许它对你也有用。