У меня есть пользовательский RelativeLayout в remote_image_view.xml
:Пользовательские RelativeLayouts в layout.xml
<?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, а не в коде.
+1 вы свеклы меня ... –
спасибо, ребята! Я даже не знал, что это возможно ... – efpies
Теперь IDEA показывает мне ошибку: 'Не удается выполнить рендеринг: java.lang.ClassCastException: com.android.layoutlib.bridge.MockView не может быть передан в android.view.ViewGroup'. Если 'ImageView' удален из' RemoteImageView', он показывает 'Отсутствует класс com.dmact.remoteimage.RemoteImageView' – efpies