Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.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_View - Fatal编程技术网

Java 从xml创建自定义视图

Java 从xml创建自定义视图,java,android,xml,view,Java,Android,Xml,View,我想做一个来自XML的自定义视图 以下是我的XML: <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/movieTitle" android:text="@string/movietitle" android:textS

我想做一个来自XML的自定义视图

以下是我的XML:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <TextView 
        android:id="@+id/movieTitle"
        android:text="@string/movietitle"
        android:textSize="35dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        />
    <com.galite.headliner.views.LoaderImageView
        android:id="@+id/moviePoster" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/movieTitle"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:contentDescription="@string/poster"
        />
    <TextView
        android:id="@+id/movieDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/description"
        android:layout_below="@id/moviePoster"
        android:gravity="center"
        />
</RelativeLayout>

如何使MyView等于v?

您必须将膨胀视图
v
连接到
MyView
,如下所示:

v = inflater.inflate(R.layout.movie, this, true);
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
    <TextView 
        android:id="@+id/movieTitle"
        android:text="@string/movietitle"
        android:textSize="35dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        />
    <com.galite.headliner.views.LoaderImageView
        android:id="@+id/moviePoster" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/movieTitle"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:contentDescription="@string/poster"
        />
    <TextView
        android:id="@+id/movieDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/description"
        android:layout_below="@id/moviePoster"
        android:gravity="center"
        />
</merge>
如果您想要精确的布局,请制作
MyView
以扩展
RelativeLayout
,并修改xml布局,如下所示:

v = inflater.inflate(R.layout.movie, this, true);
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
    <TextView 
        android:id="@+id/movieTitle"
        android:text="@string/movietitle"
        android:textSize="35dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        />
    <com.galite.headliner.views.LoaderImageView
        android:id="@+id/moviePoster" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/movieTitle"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:contentDescription="@string/poster"
        />
    <TextView
        android:id="@+id/movieDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/description"
        android:layout_below="@id/moviePoster"
        android:gravity="center"
        />
</merge>

类型LayoutFlater中的方法充气(int、ViewGroup、boolean)不适用于参数(int、MovieView、boolean)