首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过使用xml设计来制作自定义组件

通过使用xml设计来制作自定义组件
EN

Stack Overflow用户
提问于 2013-07-31 04:36:55
回答 1查看 139关注 0票数 2

经过大量研究,我不知道如何在Xml中设计定制组件。

我知道我必须创建一个继承View的类,但在构造函数中,我想引用一个xml,在这个xml中,我使用一些android组件设计我的对象。

这是我放在布局文件夹的chronometer.xml文件中的视图内容:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<com.fr.loroux.minuteursimple.Chronometer
 xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/customChronometer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/hours"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00" />

    <TextView
        android:id="@+id/minutes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00" />

    <TextView
        android:id="@+id/seconds"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00" />

</com.fr.loroux.minuteursimple.Chronometer>

但是现在我如何通过构造函数类引用它呢?这是不可能的?

谢谢你的帮助。

EN

Stack Overflow用户

发布于 2013-07-31 05:13:10

您想要做的是一个复合视图。要引用xml布局,请在构造函数中使用以下代码:

代码语言:javascript
运行
复制
LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.chronometer, this, true);

有关如何创建视图的更多信息,请参见Create compound view

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17956875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档