我用ImageView创建了一个布局,我想用两个不同的ImageView包含它两次 就像具有不同位置的ListView一样。
这是ImageView布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/main_guilds_item"
android:layout_gravity="center_horizontal" />
<include
layout="@layout/main_guilds_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
这里我包括两次布局但我想访问那些具有不同ID的ImageView来设置不同的图像。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/main_guilds_item"
android:layout_gravity="center_horizontal" />
<include
layout="@layout/main_guilds_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
我该怎么做?
答案 0 :(得分:0)
您可以为此目的创建自定义视图。步骤
创建视图my_custom_view.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
/>
</RelativeLayout>
然后你需要创建一个扩展RelativeLayout的类ItemList
(my_custom_view.xml中的父布局)
public class ItemList extends RelativeLayout {
ImageView mImageView;
public ItemList(Context context) {
this(context, null);
}
public ItemList(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ItemList(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
LayoutInflater.from(context).inflate(R.layout.item_list_content, this);
mImageView=(ImageView) findViewById(R.id.image_view);
}
public void setImage(Drawable imageId){
mImageView.setImageDrawable(imageId);
}
}
然后您可以在其他布局中多次使用此自定义视图,如下所示
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<yourpackage.ItemList
android:id="@+id/first_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</yourpackage.ItemList>
<yourpackage.ItemList
android:id="@+id/second_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</yourpackage.ItemList>
</LinearLayout>
现在你上课了
ItemList firstItem=(ItemList) findViewById(R.id.first_view);
ItemList secondItem=(ItemList) findViewById(R.id.second_view);
firstItem.setImage(R.drawable.your_image);
secondItem.setImage(R.drawable.your_image_2);
注意:强> 您也可以从xml中获取图像。在这种情况下,您将不得不使用自定义属性。为此目的,请点击链接。 https://stackoverflow.com/a/7608739/3975838