Java 在XML中创建自定义布局,以便在代码或其他布局XML中初始化

Java 在XML中创建自定义布局,以便在代码或其他布局XML中初始化,java,android,xml,android-layout,android-custom-view,Java,Android,Xml,Android Layout,Android Custom View,在Android中创建自定义布局时,我遇到了一个问题,我正试图解决这个问题(或者更好地理解应该怎么做) 我想创建一个自定义的RelativeLayout类供我使用,该类在布局XML文件中定义 my\u relative\u layout.xml <?xml version="1.0" encoding="utf-8"?> <com.mypackage.MyRelativeLayout xmlns:android="http://schemas.android.com/apk/r

在Android中创建自定义布局时,我遇到了一个问题,我正试图解决这个问题(或者更好地理解应该怎么做)

我想创建一个自定义的
RelativeLayout
类供我使用,该类在布局XML文件中定义

my\u relative\u layout.xml

<?xml version="1.0" encoding="utf-8"?>
<com.mypackage.MyRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:scaleType="fitStart"
        android:src="@drawable/my_drawable"/>
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/title_gray"
        android:layout_below="@id/image_view"
        android:gravity="center"
        android:text="@string/placeholder" />
</com.mypackage.MyRelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:scaleType="fitStart"
        android:src="@drawable/my_drawable"/>
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/title_gray"
        android:layout_below="@id/image_view"
        android:gravity="center"
        android:text="@string/placeholder" />
</RelativeLayout>
我的问题是我想在另一个XML和代码中初始化这个布局。 但在编写类和XML时,我只能通过以下操作在代码中使用它:

myLayout = (MyRelativeLayout) LayoutInflater.from(this.getActivity()).inflate(R.layout.my_relative_layout, container, false);
在另一个XML中写入以下内容时,会导致onFinishInflate在getViewById上失败(返回null),并且childrenCount为0

<com.mypackage.MyRelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@id/another_layout"
    app:image_view="@drawable/my_image" />
但是XML不会引用我的类。
我的问题是,
1.自定义布局的定义中是否缺少某些内容?
2.自定义布局的正确定义是什么?


提前谢谢你

首先,您应该在contractor中使用样式化属性,如示例所示

你怎么能期望MyRelativeLayout定义为休耕:

<com.mypackage.MyRelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@id/another_layout"
    app:image_view="@drawable/my_image" />
您的服装视图

public class MyRelativeLayout extends RelativeLayout {
        private AttributeSet attrs;
        private ImageView imageView;
        private TextView textView;

        public MyRelativeLayout(Context context) {
            super(context);
            init();
        }

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

        public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }

        private void init(Context context){
            LayoutInflater.from(context).inflate(R.layout.costume_layout, this);
        }
}
<com.mypackage.MyRelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@id/another_layout"
    app:image_view="@drawable/my_image" />
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:scaleType="fitStart"
        android:src="@drawable/my_drawable"/>
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/title_gray"
        android:layout_below="@id/image_view"
        android:gravity="center"
        android:text="@string/placeholder" />
</RelativeLayout>
public class MyRelativeLayout extends RelativeLayout {
        private AttributeSet attrs;
        private ImageView imageView;
        private TextView textView;

        public MyRelativeLayout(Context context) {
            super(context);
            init();
        }

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

        public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }

        private void init(Context context){
            LayoutInflater.from(context).inflate(R.layout.costume_layout, this);
        }
}