使用常规 Android 组件创建自定义组件
基本上我想从我已有的代码中封装一个简单的组件。
基本上它是一个带有按钮的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您甚至可以在发送点击之前对视图执行某些操作,如下所示:
You can even do something to the view before dispatching the click like this: