经过大量研究,我不知道如何在Xml中设计定制组件。
我知道我必须创建一个继承View的类,但在构造函数中,我想引用一个xml,在这个xml中,我使用一些android组件设计我的对象。
这是我放在布局文件夹的chronometer.xml文件中的视图内容:
<?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>但是现在我如何通过构造函数类引用它呢?这是不可能的?
谢谢你的帮助。
发布于 2013-07-31 05:13:10
您想要做的是一个复合视图。要引用xml布局,请在构造函数中使用以下代码:
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.chronometer, this, true);有关如何创建视图的更多信息,请参见Create compound view。
https://stackoverflow.com/questions/17956875
复制相似问题