安卓:如何使用XML进行自定义视图布局 [英] Android: how to use XML for custom view layout
问题描述
我是从GWT和UiBinder的,的世界,在那里,我通过扩展复合,然后把所有的UI布局code在MyComponent.ui.xml文件中用于定义自定义组件来了。这是建立在更小的部分组件的非常好的方式。
I'm coming from the world of GWT and UIBinder, where I'm used to defining custom components by extending Composite, and then putting all the UI layout code in a MyComponent.ui.xml file. This is a very nice way to build up components from smaller pieces.
我试图实现与自定义视图在Android同样的效果。我已经能够通过编程扩展查看
,然后通过调用 addView(TextView中)
添加对象。我希望能够做到这一点的XML,但我不明白如何将XML布局文件的视图(除关联的主 RES /布局/ main.xml中
文件,该文件提供了应用程序的主要布局。
I'm trying to achieve the same effect on Android with custom Views. I've been able to programmatically extend View
, and then add objects by calling addView(textView)
. I'd like to be able to do that in XML, but I don't see how to associate an xml layout file with the view (apart the primary res/layout/main.xml
file, which provides the primary layout for the app.
我该如何布局我的自定义视图中的XML?
How can I layout my custom views in XML?
编辑:我的问题是不清楚。什么我希望做的是一个 my_widget.xml
文件与我的自定义视图相关联。然后在my_widget.xml我想定义各种TextViews等,并将其插入我的视图类。
My question was unclear. What I'm looking to do is associate a my_widget.xml
file with my customized view. Then in my_widget.xml I'd like to define various TextViews, etc, and plug them into my View class.
推荐答案
要构建从XML视图,可使用的 LayoutInflater 。
To construct Views from XML, use LayoutInflater.
这篇关于安卓:如何使用XML进行自定义视图布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!