我想将gridview设置为具有平方大小的屏幕宽度/行数的单元格。我对从网上拉下来的图片有疑问。我可以在XML中对它进行硬编码并且它都可以工作,但是我需要为每个布局大小设置尺寸。有两个图像视图,一个是如果图像本身不是正方形则有背景图块。我似乎无法找到如何在代码中动态设置它。我已尝试设置最小高度,并尝试设置布局以填充父级,然后设置图像视图的大小在通胀。无论是从正确的大小开始还是失败,似乎......
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/element_spacing"
android:paddingTop="@dimen/element_spacing"
android:layout_centerHorizontal="true"
>
<ImageView
android:id="@+id/gridview_adapter_background_iv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="@dimen/element_spacing"
android:background="@color/color_primary"
android:contentDescription="@string/content_description_ui"
/>
<ImageView
android:id="@+id/gridview_adapter_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/element_spacing"
android:layout_centerInParent="true"
android:contentDescription="@string/content_description_result"
/>
<ProgressBar
android:id="@+id/grid_adapter_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
获取视图..
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ImageDataViewHolder imageDataViewHolder;
/*
* see if we need to inflate the view, or if we can make use of the view holder pattern
*/
if (convertView == null) {
convertView = inflater.inflate(R.layout.adapter_gridview, parent, false);
imageDataViewHolder = new ImageDataViewHolder();
imageDataViewHolder.gridview_adapter_iv = (ImageView) convertView.findViewById(R.id.gridview_adapter_iv);
imageDataViewHolder.gridview_adapter_background_iv = (ImageView) convertView.findViewById(R.id.gridview_adapter_background_iv);
imageDataViewHolder.grid_adapter_spinner = (ProgressBar) convertView.findViewById(R.id.grid_adapter_spinner);
} else {
imageDataViewHolder = (ImageDataViewHolder) convertView.getTag();
}
/*
* Set the UI elements, reset the spinner
*/
imageDataViewHolder.position = position;
imageDataViewHolder.grid_adapter_spinner.setVisibility(View.VISIBLE);
final String url = mValues.get(position).getUrl();
imageDataViewHolder.gridview_adapter_iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mCallbacks.click(url);
}
});
convertView.setTag(imageDataViewHolder);
/*
* Process the bitmap. If it exists, set it. If not, create a volley request to try to access
* it. If it succeeds, save the bitmap to the cache. If it fails put up error icon.
*/
Bitmap bm = mCache.get(url);
if (bm != null) {
imageDataViewHolder.gridview_adapter_iv.setImageBitmap(bm);
imageDataViewHolder.grid_adapter_spinner.setVisibility(View.INVISIBLE);
} else {
imageDataViewHolder.gridview_adapter_iv.setImageBitmap(null);
ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
mCache.put(url, response);
/*
* Check to make sure its the same view is visible before making edits.
* Else we will get a flicker effect as different images are downloaded.
*
*/
if (imageDataViewHolder.position == position) {
imageDataViewHolder.gridview_adapter_iv.setImageBitmap(response);
imageDataViewHolder.grid_adapter_spinner.setVisibility(View.INVISIBLE);
}
}
}, mSize, mSize, null, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
imageDataViewHolder.grid_adapter_spinner.setVisibility(View.INVISIBLE);
imageDataViewHolder.gridview_adapter_iv.setImageResource(R.drawable.ic_error_outline_white_24dp);
}
});
mVolleySingleton.addRequest(imageRequest);
}
return convertView;
}
答案 0 :(得分:0)
您最好的选择是继承RelativeLayout
并覆盖onMeasure()
:
public class SquareRelativeLayout extends RelativeLayout{
public SquareRelativeLayout (Context context) {
super(context);
}
public SquareRelativeLayout (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public SquareRelativeLayout (Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}
这适用于GridView
,因为它将使用MeasureSpec.EXACTLY
,IIRC模式。如果它不起作用,您可以使用此处的建议调整代码:Simple way to do dynamic but square layout
顺便说一句,您应该可以通过设置背景使用单个ImageView
,然后为位图指定android:scaleType="fitCenter"
。