Android 通过在xml中进行设计来创建自定义组件
经过大量研究,我没有发现如何用Xml设计自定义组件 我知道我必须创建一个继承视图的类,但在构造函数中我想引用一个xml,在这里我使用一些android组件设计我的对象 这是我放在布局文件夹中的chronometer.xml文件中的视图内容:Android 通过在xml中进行设计来创建自定义组件,android,android-xml,android-custom-view,Android,Android Xml,Android Custom View,经过大量研究,我没有发现如何用Xml设计自定义组件 我知道我必须创建一个继承视图的类,但在构造函数中我想引用一个xml,在这里我使用一些android组件设计我的对象 这是我放在布局文件夹中的chronometer.xml文件中的视图内容: <?xml version="1.0" encoding="utf-8"?> <com.fr.loroux.minuteursimple.Chronometer xmlns:android="http://schemas.android.
<?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>
但是现在我如何通过构造函数类引用它呢?
不可能吗
谢谢您的帮助。您要做的是复合视图。要引用xml布局,请使用构造函数中的下一个代码:
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.chronometer, this, true);
有关如何创建视图的更多信息,请参见。或更短的内容:
布局更平坦。从(上下文)。充气(R.layout.chronometer,this,true)
或更短,最后一个true
参数是可选的,默认为false.)