简体   繁体   中英

Android: Creating custom view and adding it to layout dynamically

I want to create a custom view with some text and two buttons all on one line. I need to be able to add multiple (any number) of these views to an existing layout dynamically (needs to be able to scroll). I want to pass a custom object to the view and set the text and buttons. I need access to the button event handlers from the activity. I've looked a little into custom views but I'm still at a loss for how to do what I want. I'm used to .NET custom controls, and I'm looking for the same effect. Any help or example code would be greatly appreciated.

What you want is custom compound view. You should write you own class (usually extending one of the layouts) and whole behavior, inflate the layout the way you want etc.

More of it: http://developer.android.com/guide/topics/ui/custom-components.html

This one helped me a lot too: http://javatechig.com/android/creating-custom-and-compound-views-in-android-tutorial

If you use list activity or list fragment, you will automatically have the many features you have asked for. You only need to create a adapter class for your listview. You can define your layout for your row view(buttons, text etc..) Try to look at the examples on the web for cusom adapter and lists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM