Android-GridView 填充问题
我正在开发一个应用程序,将图像加载到 GridView 中...我现在的问题是 当我使用 imageView.setPadding(10,10,10,10)
时,它对于 HTC Hero 工作正常,但对于 Motorola 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(左、上、右、下);
imageView.setLayoutParams(lp);
那么还有其他方法可以做吗..或者我在哪里错了.. 帮帮我吧..
提前致谢。
Im working on a app where im loading an image into the GridView
... My problem now is
when im using the imageView.setPadding(10,10,10,10)
its working fine for HTC Hero but in case of Motorola Droid they are overlapping each other...
How to make the padding to be common for all mobile...
Here is my code..
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;
}
}
I have also tried to use theint w = gridView.getWidth();
int myViewWidth = Math.round(W * .12f);
and also i tried with theLinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);
So is there anyother way to do.. Or am i wrong anywhere..
Help me out..
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要以
dips
(与密度无关的像素)定义填充,然后将dips
转换为像素,而不是以像素为单位定义填充
在运行时。因此,您需要执行类似于
您在构造函数中获得的
scale
的操作,该比例将根据您的应用程序运行的设备的屏幕密度而有所不同。(注:
dip
和dp
是同一个东西)Rather defining the padding in
pixels
, you need to define it indips
(density independent pixels) and then convert thedips
topixels
at run time.So you would need to do something similar to
The
scale
you get in your constructor will differ depending on the density of the screen of the device your app is running on.(Note:
dip
anddp
are the same thing)