layout.xml中的自定义RelativeLayouts

时间:2012-06-06 14:02:23

标签: android relativelayout

我在remote_image_view.xml中有自定义RelativeLayout:

<?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"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/placeholder"
        />
</RelativeLayout>

好的,现在我想把它包含在main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical" android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <Button android:text="Start Download" android:id="@+id/button1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:onClick="onClick" />
    <Button android:text="View Downloads" android:id="@+id/button2"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:onClick="showDownload" />
    <ScrollView
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        >
        <LinearLayout
                android:id="@+id/lay"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:orientation="vertical"
                >
            <include layout="@layout/remote_image_view" android:id="@+id/imageView1" />
            <include layout="@layout/remote_image_view" android:id="@+id/imageView2"/>
            <include layout="@layout/remote_image_view" android:id="@+id/imageView3"/>
            <include layout="@layout/remote_image_view" android:id="@+id/imageView4"/>
            <include layout="@layout/remote_image_view" android:id="@+id/imageView5"/>
            <include layout="@layout/remote_image_view" android:id="@+id/imageView6"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

那很好。所以我创建了一个班级RemoteImage

public class RemoteImageView extends RelativeLayout {

    public RemoteImageView(Context context) {
        super(context);
    }

    public void init(RelativeLayout.LayoutParams layoutParams) {
        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.remote_image_view, this);
        setLayoutParams(layoutParams);
    }
}

出现了一个疯狂的问题。 我不能编写如下代码:

RemoteImageView rm1 = (RemoteImageView)findViewById(R.id.imageView1);

因为我会得到例外:

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to com.dmact.remoteimage.RemoteImageView

因此,我应该动态创建RemoteImageView的实例,然后对其应用一些LayoutParams。有没有办法不这样做?我想用XML格式化布局,而不是代码。

2 个答案:

答案 0 :(得分:7)

如果您需要使用RemoteImageView类继承RelativeLayout,您还必须在XML文件中指定您的类

<?xml version="1.0" encoding="utf-8"?>
<com.yourpackagepath.RemoteImageView
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
    <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/placeholder"
        />
</com.yourpackagepath.RemoteImageView>

答案 1 :(得分:1)

给出java文件的完整路径而不是RelativeLayout

<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"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/placeholder"
        />
</RelativeLayout>

喜欢

<com....RemoteImageView  xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content">
        <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/placeholder"
            />
    </com....RemoteImageView>