[英]Android: how to use XML for custom view layout
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. 我来自GWT和UIBinder的世界,我习惯于通过扩展Composite定义自定义组件,然后将所有UI布局代码放在MyComponent.ui.xml文件中。 This is a very nice way to build up components from smaller pieces. 这是从较小的部件构建组件的一种非常好的方法。
I'm trying to achieve the same effect on Android with custom Views. 我正在尝试使用自定义视图在Android上实现相同的效果。 I've been able to programmatically extend View
, and then add objects by calling addView(textView)
. 我已经能够以编程方式扩展View
,然后通过调用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中实现这一点,但我没有看到如何将xml布局文件与视图相关联(除了主要res/layout/main.xml
文件,它提供了应用程序的主要布局。
How can I layout my custom views in XML? 如何在XML中布局自定义视图?
Edit: My question was unclear. 编辑:我的问题不清楚。 What I'm looking to do is associate a my_widget.xml
file with my customized view. 我要做的是将my_widget.xml
文件与我的自定义视图相关联。 Then in my_widget.xml I'd like to define various TextViews, etc, and plug them into my View class. 然后在my_widget.xml中我想定义各种TextViews等,并将它们插入到我的View类中。
Use the fully qualified name of your custom view class in place of one of the built-in views. 使用自定义视图类的完全限定名称代替其中一个内置视图。 Here's a layout, for instance, that fills the window with your class: 例如,这是一个用你的类填充窗口的布局:
<?xml version="1.0" encoding="utf-8"?>
<my.package.MyCustomView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
要从XML构造视图,请使用LayoutInflater 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.