使用常规 Android 组件创建自定义组件

发布于 2024-12-10 21:33:53 字数 874 浏览 0 评论 0原文

基本上我想从我已有的代码中封装一个简单的组件。

基本上它是一个带有按钮的LinearLayout。这些按钮将对 ListView 进行更改,并且还会执行一些其他小操作。

目前我有一个 XML 布局,并且我以编程方式设置其他所有内容:按钮、列表和其他小东西之间的交互。

显然我心里想,让我们概括一下吧。

我开始尝试扩展 LinearLayout 并添加按钮。 我已经不知道如何膨胀按钮以添加到视图中 我应该重写什么方法来在创建视图之前创建此按钮,而不会干扰度量和通货膨胀等。

我环顾四周,但我看到的自定义组件要么是全新的组件,要么是简单地向视图添加小功能的组件定制的。

这样做有一些指导方针吗? 好的教程/示例?

任何帮助表示赞赏。谢谢 !

编辑:

好的,这里有一些更具体的内容。

基本上我想创建一个包含 ListView 的过滤器按钮的 View 。这将在不同的地方使用不同的过滤器,所以我需要按钮的灵活性。

基本上我想做这样的事情:

CustomView view = new CustomView(activity);
view.addButton("Lala", new OnFilterClickListener { 
    onClick(ListView list, View v) {
      // Do the filtering
    }
});

mListView.addHeaderView(view);

我希望视图适应它的权重以显示按钮,向用户显示哪个过滤器处于活动状态,诸如此类。

但我仍然不知道如何使这些动态添加的按钮出现,在哪里生成它们,如何膨胀它们等等。

basically I want to encapsulate a simple component from code that I already have.

Basically it's a LinearLayout with buttons inside. These buttons will make changes to a ListView, and there is also some other small stuff that it will do.

Currently I have a XML layout with those, and I programmatically setup everything else: the buttons, the interaction between the list and the other small stuff.

Obviously I thought to myself, let's encapsulate this.

I started out trying to extend the LinearLayout and adding the buttons.
Already I have no idea how to inflate the buttons to add to the view
What method do I override to create this buttons just before the view gets created without messing with the measures and inflations, etc.

I've looked around but the custom components I see are either completely new components or components that simply add small functionality to the custom ones.

Is there some guidelines for doing this?
Good tutorials/examples?

Any help is appreciated. Thanks !

EDIT:

Okay, here is a little more specific stuff.

Basically I want to create a View that holds filter buttons for a ListView. This will be used in different places with different filters, so I need flexibility for the buttons.

Basically I'd like to do something like this:

CustomView view = new CustomView(activity);
view.addButton("Lala", new OnFilterClickListener { 
    onClick(ListView list, View v) {
      // Do the filtering
    }
});

mListView.addHeaderView(view);

I want the view to adapt it's weights for showing the buttons, show the user which filter is active, stuff like that.

But I still don't really know how to make those dynamically added buttons appear, where do I generate them, how to inflate them and stuff like that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2024-12-17 21:33:53
public class myLayout extends LinearLayout {

    //...

    public void addButton(String text, OnClickListener listener) {
        Button newButton = new Button(mContext);
        newButton.setText(text);
        newButton.setOnClickListener(listener);
        //Say we want the weights to be equal
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.Fill_PARENT, 1);
        addView(newButton, params);
    }

    //...

}

您甚至可以在发送点击之前对视图执行某些操作,如下所示:

public class myLayout extends LinearLayout {

    //...

    public void addButton(String text, final OnClickListener listener) {
        Button newButton = new Button(mContext);
        newButton.setText(text);
        newButton.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                //do whatever you want
                //like change background of button or something
                //finally
                listener.onClick(v);
            }
        });
        //Say we want the weights to be equal
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.Fill_PARENT, 1);
        addView(newButton, params);
    }

    //...

}
public class myLayout extends LinearLayout {

    //...

    public void addButton(String text, OnClickListener listener) {
        Button newButton = new Button(mContext);
        newButton.setText(text);
        newButton.setOnClickListener(listener);
        //Say we want the weights to be equal
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.Fill_PARENT, 1);
        addView(newButton, params);
    }

    //...

}

You can even do something to the view before dispatching the click like this:

public class myLayout extends LinearLayout {

    //...

    public void addButton(String text, final OnClickListener listener) {
        Button newButton = new Button(mContext);
        newButton.setText(text);
        newButton.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                //do whatever you want
                //like change background of button or something
                //finally
                listener.onClick(v);
            }
        });
        //Say we want the weights to be equal
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.Fill_PARENT, 1);
        addView(newButton, params);
    }

    //...

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文