我正在开发一个具有gridview布局的主屏幕的应用程序。 就目前而言,我正在用DP号码硬编码盒子的高度和宽度,我显然不想这样做。如何创建将决定用户的代码'屏幕大小并相应地匹配框,以便有像笔记的大手机的人会看到与机器人mini上的人相同的东西? IE,当每边硬编码到500dp时,它看起来像这样:
但我的目标是在每个屏幕尺寸上看起来像这样:
目前,这是我的Java代码:
公共类ActivityAdapter扩展了BaseAdapter {
//Array of Icons that will be used as menu choices
public static int[] imageOptions = {
R.drawable.vacation_quota, //Position 0
R.drawable.revenue_deficit, //Position 1
//+ many more options...
};
private Context context;
//Constructor to pass context back to Main class
public ActivityAdapter(Context applicationContext) {
context = applicationContext;
}
//Number of elements to be displayed on the grid
@Override
public int getCount() {
return imageOptions.length;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ImageView iv;
if(convertView != null){
iv = (ImageView) convertView;
} else {
iv = new ImageView(context);
//Here I have it hard coded to match a 480px width. Here is where I need to change it, just not sure how
iv.setLayoutParams(new GridView.LayoutParams(240, 240));
iv.setScaleType(ScaleType.CENTER_CROP); //Center the cropping
}
//Sets the image to correspond to its position within the array.
iv.setImageResource(imageOptions[position]);
return iv;
}
有什么想法吗?我认为逻辑上我想弄清楚屏幕尺寸并使用它们来使图像尺寸匹配,只是不确定如何。来自其他线程的研究没有太大帮助,所以希望我能通过代码发布得到一个更简单的答案。
答案 0 :(得分:0)
你应该得到屏幕的宽度和高度。然后使用这些值动态设置每个单元格的高度和宽度
答案 1 :(得分:0)
试试这段代码 -
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle state) {
setContentView(new ViewGroup(this) {
private RelativeLayout[] items = new RelativeLayout[9];
private int width, height, itemWidth, itemHeight;
{
Random r = new Random();
for (int i = 0; i < 9; i++) {
items[i] = new RelativeLayout(getContext());
float[] hsv = new float[] {360 * r.nextFloat(), .50f, .75f};
items[i].setBackgroundColor(Color.HSVToColor(hsv));
addView(items[i]);
// UPDATE ////////////////////////////////////
ImageView image = new ImageView(getContext());
switch (i) {
case 0: // top left
case 1: // top center
case 2: // top right
case 3: // center left
case 4: // center center
case 5: // center right
case 6: // bottom left
case 7: // bottom center
case 8: // bottom right
image.setImageResource(R.drawable.ic_launcher);
break;
}
image.setScaleType(ScaleType.FIT_XY);
image.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
));
items[i].addView(image);
//////////////////////////////////////////////
}
}
@Override
protected void onMeasure(int wMS, int hMS) {
width = MeasureSpec.getSize(wMS);
height = MeasureSpec.getSize(hMS);
itemWidth = width / 3;
itemHeight = height / 3;
wMS = MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY);
hMS = MeasureSpec.makeMeasureSpec(itemHeight, MeasureSpec.EXACTLY);
measureChildren(wMS, hMS);
setMeasuredDimension(width, height);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
for (int i = 0; i < 9; i++) {
l = itemWidth * (i % 3);
t = itemHeight * (i / 3);
r = l + itemWidth;
b = t + itemHeight;
items[i].layout(l, t, r, b);
}
}
});
super.onCreate(state);
}
}
输出 -
编辑 -
要创建可滚动网格视图,请参阅此http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/
答案 2 :(得分:0)
我最终无法使用此处的一些建议,并且必须使用弃用的方法来保留我的应用结构。我特别添加的代码是:
iv = new ImageView(context);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
float width2 = display.getWidth();
int length = (int) (width2/2);
iv.setLayoutParams(new GridView.LayoutParams(length, length));
这样,根据屏幕的大小,高度和宽度将裁剪为正确的尺寸。