我有一个问题,它让我整天疲惫不堪。
在ImageView
中将网络图片加载到ImageView
时,为什么会显示GridView
的背景? ImageView
的大小不能包裹内容。
我该如何解决?使ImageView
的尺寸适应图片尺寸(不要改变尺寸)
谢谢你。
A。主要XML
<ListView
android:id="@+id/gridview_listview"
android:layout_width="100dp"
android:background="#00000000"
android:scrollbars="none"
android:dividerHeight="5dp"
android:divider="@color/colorAlpha"
android:listSelector="@drawable/list_item_selected"
android:layout_height="match_parent" />
<GridView
android:id="@+id/gridview"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_width="0dp"
android:scrollbars="none"
android:listSelector="@drawable/gridview_item_selected"
android:layout_height="wrap_content">
</GridView>
B.GridView Item XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:padding="1dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_gridview"
android:padding="0dp"
android:background="#ff0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_gridview"
android:text="test"
android:textColor="#00ffff"
android:textSize="15sp"
android:gravity="center"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
C.GridView适配器扩展BaseAdapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
GridViewHolder gridViewHolder = new GridViewHolder();
ImageView imageView;
TextView textView;
if (convertView == null ) {
convertView = layoutInflater.inflate(R.layout.gridview_item , null) ;
imageView = (ImageView) convertView.findViewById(R.id.iv_gridview);
textView = (TextView) convertView.findViewById(R.id.tv_gridview);
gridViewHolder.setImageView(imageView);
gridViewHolder.setTextView(textView);
convertView.setTag(gridViewHolder);
}else {
gridViewHolder = (GridViewHolder) convertView.getTag();
}
Picasso.with(context).load("http://img1.cache.netease.com/catchpic/A/A8/A86B03AE11945B98D0AE0A98480D28CE.jpg").into(gridViewHolder.getImageView());
gridViewHolder.getTextView().setText(dataList.get(position).getName());
return convertView;
}
D.活动代码
dataList = GetData.getData();
GridViewAdapter gridViewAdapter = new GridViewAdapter(GridView.this , dataList);
gridView.setAdapter(gridViewAdapter);
gridView.setNumColumns(3);
gridView.setHorizontalSpacing(5);
gridView.setVerticalSpacing(10);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String url = "this is " + position ;
startActivity(new Intent(GridView.this , ClickActivity.class).putExtra("url" ,url));
}
});
答案 0 :(得分:0)
尝试在布局XML中将android:adjustViewBounds
的{{1}}属性设置为ImageView
。
true
来自<ImageView
android:id="@+id/iv_gridview"
android:padding="0dp"
android:background="#ff0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
上的documentation:
如果希望ImageView调整其边界,请将此项设置为true 保留其可绘制的宽高比。