android 开发中GridView中如何添加一张图片 addview 最好是用HorizontalScrollView 里面含有一个线性布局,线性布局.addView(img) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 大哥,你说的是在哪加addView,在xml里面吗 <GridView android:id="@+id/grv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:horizontalSpacing="1sp" android:verticalSpacing="1sp" android:numColumns="6" android:fadeScrollbars="true" android:background="@drawable/sns_shoot_add_bg" android:gravity="center"> </GridView>还是在java文件中//初始化添加多张图片组件 grv_content = (GridView)findViewById(R.id.grv_content); adapter = new ImageAdapter(this, datas); grv_content.setAdapter(adapter);//图片就是这样加载到gridview里面的 //注册grv_content监听 registerListener(); 把添加的view看做你的item加进去就可以了 有木有类似的demo给发个链接,学习一下。 我帮你做了个动态向GridView添加图片的APK 需要的话可以M我想怎么添加就怎么添加 xml配置如下:<RelativeLayout android:id="@+id/photo_post_select_grid_frame" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" > <com.gf.component.widget.ExtendGridView android:id="@+id/photo_post_select_grid" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/gf_inputbox_upload_pic" android:fadingEdge="none" android:gravity="center" android:horizontalSpacing="4dip" android:listSelector="@color/transparent" android:padding="10dip" android:scrollbarStyle="insideOverlay" android:scrollbarThumbVertical="@drawable/gf_scrollbar" android:scrollingCache="false" android:stretchMode="columnWidth" android:verticalSpacing="4dip" gf:stretchable="true" /> </RelativeLayout>其中ExtendGridView实现如下public class ExtendGridView extends GridView { private int mHorizontalSpacing; private int mVerticalSpacing; private int mNumColumns; private boolean mStretchable; private boolean mPenetrateTouch; private int mScreenWidth, mScreenHeight; public ExtendGridView(Context context) { this(context, null); } public ExtendGridView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ExtendGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GridView); mStretchable = a.getBoolean(R.styleable.GridView_stretchable, false); mPenetrateTouch = a.getBoolean(R.styleable.GridView_penetrateTouch, false); a.recycle(); init(); } @Override public void setHorizontalSpacing(int horizontalSpacing) { super.setHorizontalSpacing(horizontalSpacing); mHorizontalSpacing = horizontalSpacing; } public int getHorizontalSpacing() { return mHorizontalSpacing; } @Override public void setVerticalSpacing(int verticalSpacing) { super.setVerticalSpacing(verticalSpacing); mVerticalSpacing = verticalSpacing; } public int getVerticalSpacing() { return mVerticalSpacing; } @Override public void setNumColumns(int numColumns) { super.setNumColumns(numColumns); mNumColumns = numColumns; } public int getNumColumns() { return mNumColumns; } public void setStretchable(boolean stretchable) { if (mStretchable != stretchable) { mStretchable = stretchable; requestLayout(); } } public boolean isStretchable() { return mStretchable; } public void setPenetrateTouch(boolean penetrate) { mPenetrateTouch = penetrate; } public boolean isPenetrateTouch() { return mPenetrateTouch; } public int computeItemWidth() { int width = getWidth() > 0 ? getWidth() : getMeasuredWidth(); // use screen width as max width. if (width <= 0) width = mScreenWidth; int horizontalSpace = getHorizontalSpacing(); int horizontalPadding = getPaddingLeft() + getPaddingRight(); int columnsNum = getNumColumns(); int itemSize = (width - horizontalPadding - (columnsNum - 1) * horizontalSpace) / columnsNum; if (itemSize < 0) { return LayoutParams.WRAP_CONTENT; } return itemSize; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final boolean stretchable = mStretchable; if (stretchable) { heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @TargetApi(Build.VERSION_CODES.FROYO) @Override protected void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); updateScreenSize(); } @Override public boolean onTouchEvent(MotionEvent ev) { // final int action = ev.getAction(); final int x = (int) ev.getX(); final int y = (int) ev.getY(); int motionPosition = pointToPosition(x, y); if (mPenetrateTouch && motionPosition < 0) { return false; } return super.onTouchEvent(ev); } private void init() { updateScreenSize(); } private void updateScreenSize() { Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); mScreenWidth = display.getWidth(); mScreenHeight = display.getHeight(); }}然后接下来是我们的主代码viewGridView = (ExtendGridView) findViewById(R.id.photo_post_select_grid); viewGridView.setNumColumns(GRID_COLUMN_COUNT); viewGridView .setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (adapter.isExtra(position)) { if (getSelectedImageList().size() < getMaxPhotoCnt()) { showSelectImageDialog(); } } else { // 大图预览 onClickPhotoPreview(viewGridView, position, getSelectedImageList()); } } }); GridView里面根本没有addView这个方法,你可以查一查API gridview 的item(这个是一个view),怎么会没有addview的方法 谁有demo,求 发份啊。 谢谢[email protected] demo 有个,我不是有下载链接么,那个还是比人在论坛要的我上传到资源里了啊http://download.csdn.net/detail/qq153843338/6674245 如何用canvas在自定义view里画图 android 4.0模拟器问题! 求助:checkbox怎么把原来的那个带对勾的图片去掉或者换成其他的? Activity 中 onTouchEvent 没有执行... android流量统计问题 C# JAVA Android的苦恼 android新手求大神指点 android调用webservice报错 服务器未能识别http头soapaction的值 新手请教!!Activity跳转问题 AbsListView onitemclick 问题 手机睡眠时关闭GPU是在安卓上层操作的吗? 【求助】安卓开发中使用9patch图片出现显示不一样的情况
<GridView
android:id="@+id/grv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:horizontalSpacing="1sp"
android:verticalSpacing="1sp"
android:numColumns="6"
android:fadeScrollbars="true"
android:background="@drawable/sns_shoot_add_bg"
android:gravity="center">
</GridView>
还是在java文件中
//初始化添加多张图片组件
grv_content = (GridView)findViewById(R.id.grv_content);
adapter = new ImageAdapter(this, datas);
grv_content.setAdapter(adapter);//图片就是这样加载到gridview里面的
//注册grv_content监听
registerListener();
<RelativeLayout
android:id="@+id/photo_post_select_grid_frame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" > <com.gf.component.widget.ExtendGridView
android:id="@+id/photo_post_select_grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/gf_inputbox_upload_pic"
android:fadingEdge="none"
android:gravity="center"
android:horizontalSpacing="4dip"
android:listSelector="@color/transparent"
android:padding="10dip"
android:scrollbarStyle="insideOverlay"
android:scrollbarThumbVertical="@drawable/gf_scrollbar"
android:scrollingCache="false"
android:stretchMode="columnWidth"
android:verticalSpacing="4dip"
gf:stretchable="true" />
</RelativeLayout>
其中ExtendGridView实现如下
public class ExtendGridView extends GridView { private int mHorizontalSpacing;
private int mVerticalSpacing;
private int mNumColumns; private boolean mStretchable;
private boolean mPenetrateTouch; private int mScreenWidth, mScreenHeight; public ExtendGridView(Context context) {
this(context, null);
} public ExtendGridView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public ExtendGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GridView);
mStretchable = a.getBoolean(R.styleable.GridView_stretchable, false);
mPenetrateTouch = a.getBoolean(R.styleable.GridView_penetrateTouch, false);
a.recycle(); init();
} @Override
public void setHorizontalSpacing(int horizontalSpacing) {
super.setHorizontalSpacing(horizontalSpacing);
mHorizontalSpacing = horizontalSpacing;
} public int getHorizontalSpacing() {
return mHorizontalSpacing;
} @Override
public void setVerticalSpacing(int verticalSpacing) {
super.setVerticalSpacing(verticalSpacing);
mVerticalSpacing = verticalSpacing;
} public int getVerticalSpacing() {
return mVerticalSpacing;
} @Override
public void setNumColumns(int numColumns) {
super.setNumColumns(numColumns);
mNumColumns = numColumns;
} public int getNumColumns() {
return mNumColumns;
} public void setStretchable(boolean stretchable) {
if (mStretchable != stretchable) {
mStretchable = stretchable;
requestLayout();
}
} public boolean isStretchable() {
return mStretchable;
} public void setPenetrateTouch(boolean penetrate) {
mPenetrateTouch = penetrate;
} public boolean isPenetrateTouch() {
return mPenetrateTouch;
} public int computeItemWidth() {
int width = getWidth() > 0 ? getWidth() : getMeasuredWidth();
// use screen width as max width.
if (width <= 0) width = mScreenWidth;
int horizontalSpace = getHorizontalSpacing();
int horizontalPadding = getPaddingLeft() + getPaddingRight();
int columnsNum = getNumColumns(); int itemSize = (width - horizontalPadding - (columnsNum - 1) * horizontalSpace) / columnsNum;
if (itemSize < 0) {
return LayoutParams.WRAP_CONTENT;
} return itemSize;
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final boolean stretchable = mStretchable;
if (stretchable) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} @TargetApi(Build.VERSION_CODES.FROYO)
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
updateScreenSize();
} @Override
public boolean onTouchEvent(MotionEvent ev) {
// final int action = ev.getAction();
final int x = (int) ev.getX();
final int y = (int) ev.getY();
int motionPosition = pointToPosition(x, y);
if (mPenetrateTouch && motionPosition < 0) {
return false;
}
return super.onTouchEvent(ev);
} private void init() {
updateScreenSize();
} private void updateScreenSize() {
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
mScreenWidth = display.getWidth();
mScreenHeight = display.getHeight();
}
}
然后接下来是我们的主代码
viewGridView = (ExtendGridView) findViewById(R.id.photo_post_select_grid);
viewGridView.setNumColumns(GRID_COLUMN_COUNT);
viewGridView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (adapter.isExtra(position)) {
if (getSelectedImageList().size() < getMaxPhotoCnt()) {
showSelectImageDialog();
}
} else {
// 大图预览
onClickPhotoPreview(viewGridView, position,
getSelectedImageList());
}
}
});