我正在开发一个应用程序,我将图像加载到GridView
...我现在的问题是
当我使用imageView.setPadding(10,10,10,10)
它对HTC Hero工作正常但是在摩托罗拉Droid的情况下它们相互重叠......
如何使填充对所有移动设备都很常见......
这是我的代码..
package com.android.sampleDesign1;
import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class TicTacToeAdapter extends BaseAdapter {
public ImageView imageView;
private Context mContext;
private Integer mThumbIds = R.drawable.images;
private Integer image;
public TicTacToeAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 9;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
image = mThumbIds;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(image);
imageView.setPadding(10,10,10,10);
} else {
imageView = (ImageView) convertView;
imageView.setImageResource(image);
}
return imageView;
}
}
我也尝试过使用
int w = gridView.getWidth();
int myViewWidth = Math.round(W * .12f);
我也试过了
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);
还有其他方法可做..或者我错了任何地方.. 帮帮我..
提前致谢。
答案 0 :(得分:4)
而是在pixels
中定义填充,您需要在dips
(密度无关像素)中定义填充,然后在运行时将dips
转换为pixels
。
所以你需要做类似的事情
private static final float PADDING_IN_DP = 10.0f; // 1 dip = 1 pixel on an MDPI device
private final int mPaddingInPixels;
public TicTacToeAdapter(Context context) {
....
// Convert the dps to pixels
final float scale = context.getResources().getDisplayMetrics().density;
mPaddingInPixels = (int) (PADDING_IN_DP * scale + 0.5f);
}
...
public View getView(int position, View convertView, ViewGroup parent) {
...
if (convertView == null) {
...
imageView.setPadding(mPaddingInPixels, mPaddingInPixels, mPaddingInPixels, mPaddingInPixels);
}
...
}
...
您在构造函数中获得的scale
将根据您的应用运行设备的屏幕密度而有所不同。
(注意:dip
和dp
是相同的事情)