【发布时间】:2013-11-10 23:19:45
【问题描述】:
此图像显示了我想要实现的布局,黑色方块是可绘制文件夹中的图像,红色方块是一个框架布局,其中 2 个图像相互叠加。问题是这样的:
红色方块(框架布局)没有出现。请参阅下面的代码...
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
我从Android Developers 得到了这个运行良好的代码,它在我的程序中运行良好,问题是我想更改这一行中的一些项目:
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
我将R.drawable.sample_2 更改为R.layout.some_layout
这里是some_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/frame"
android:layout_width="wrap_content"
android:layout_height="115dp"
android:layout_margin="2dp"
android:layout_weight="0.33" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="@drawable/imagebehind" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/frameimage" />
</FrameLayout>
</LinearLayout>
出现下一行的所有项目:
private Integer[] mThumbIds = {
R.layout.some_layout, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
R.layout.some_layout 除外。
【问题讨论】:
-
这个
R.layout.some_layout你需要提供一个drawable。所以它错了 -
有没有办法可以使用布局而不是可绘制项目? @Raghunandan
-
您可以为 gridview 项目添加自定义布局,但不能用这样的布局替换可绘制对象
-
是这样吗?好的,谢谢@Raghunandan,我会这样做的。
标签: android xml layout gridview