Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用xml文件创建自定义组件_Java_Android_Xml_File_Custom Component - Fatal编程技术网

Java 如何使用xml文件创建自定义组件

Java 如何使用xml文件创建自定义组件,java,android,xml,file,custom-component,Java,Android,Xml,File,Custom Component,我有一个xml文件,我想通过使用扩展视图的类将其更改为组件。 我该怎么做 java代码: public class custom extends View { public custom(Context context) { super(context); } public custom(Context context , AttributeSet attrs) { super(context , attrs); } } 这是我的xml代码: <RelativeLa

我有一个xml文件,我想通过使用扩展视图的类将其更改为组件。 我该怎么做

java代码:

public class custom extends View {

public custom(Context context) {
    super(context);

}

public custom(Context context , AttributeSet attrs) {
    super(context , attrs);
}
}

这是我的xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.apd.ecryptfolders.custom">

<View
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"/>


谢谢

您必须创建一个扩展布局根视图的类,然后扩展xml布局

public class CustomView extends RelativeLayout {
    public CustomView(Context context) {
        super(context);
        View view = inflate(getContext(), R.layout.layout_name, null);
        addView(view);
        // init layout
    }
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = inflate(getContext(), R.layout.layout_name, null);
        addView(view);
        // here you can apply custom attributes
        // init layout
    }
}
这些链接可以帮助您: