自定义的屏幕适配方法

在做项目时,一个app往往都需要进行‘活’的显示,而不是固定的宽高,比如在用gridview去显示图片时:

当然,列数是固定的,但是我们需要的是图片的宽高比是不会变得,图片的大小还是得做活的。所以就有如下配置

假定图片的宽高比为 16:9 则 :

 1  item_Relatlayout = (RelativeLayout)itemView.findViewById(R.id.item_Relatlayout);
 2             imageView  = (ImageView)itemView.findViewById(R.id.imageView);
 3             playBtn = (TextView)itemView.findViewById(R.id.play_tub);
 4             del_box =(CheckBox) itemView.findViewById(R.id.del_ckBox);
 5             int pxWidth =  Util.getScreanWith(context);
 6             int pxMargin = Util.dp2px(context,0.1f);
 7             GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams)item_Relatlayout.getLayoutParams();
 8             layoutParams.setMargins(pxMargin,pxMargin,pxMargin,pxMargin);
 9             item_Relatlayout.setLayoutParams(layoutParams);
10             //总宽度减去所有间隙的值
11             int t =  pxWidth - pxMargin*6;
12             //再算的图片宽度
13             RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)imageView.getLayoutParams();
14             params.width = t/3;
15             params.height = (int)((t/3) * (0.5));//计算了宽高比
16             imageView.setLayoutParams(params);

只有这样,图片的大小才会跟着不同的屏幕而进行变化,但是尺寸比例还是不变的。。。

posted @ 2015-10-16 18:12  掏富小牛  阅读(251)  评论(0编辑  收藏  举报