zoukankan      html  css  js  c++  java
  • Android GridView 指定行数,动态行宽,占满空间

    有时间我们需要 使用GridViw 让它占满父控件,例:


    特别是在适配的时间比较麻烦,在不同的机型上可能分出下,下面空的太多,或有滚动条问题,;

    下面说一下实现思路:

    首先,设置GridView 为三列,出来的为二行三列;

    我们在Adapter getView 中判断分辨率,得到宽度和高度,GridView 占总高度的八分中的三分;

    设置  Item 即每个子项的高占的空间八分中的三分的一半,宽占屏幕的三分之一;

    其次把哪么把图片设置为Item高度的一半,即 ScreenHight/8*3/2/2;

    宽度为Item宽度的二分之一,图片缩放类似为 CENTER_INSIDE

    这样,基本就可以占满全空间了;

    下面看代码:

    设置Item:

    int screenWidth = ScreenUtils.getScreenWidth();
    int screenHeight = ScreenUtils.getScreenHeight();
    //每个子项占宽度的三分之一,高度是宽屏的分七分,再占三分中的二分之一,还需要减去下面toolbar
    int viewWidth = screenWidth/3;
    int viewHeight = screenHeight*3/(2*8);
    //L.e("screenWidth:" + screenWidth);
    if (view == null) {
        view = View.inflate(mContext, R.layout.fragment_item_mine_grid, null);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(viewWidth,viewHeight);
         view.setLayoutParams(params);
    }
    设置图片:

    ViewGroup.LayoutParams ps = imageView.getLayoutParams();
    ps.width = viewWidth/2;
    ps.height = viewHeight/2;
    imageView.setLayoutParams(ps);
    //imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    Mine_GridItem ci = mList.get(i);
    textView.setText(ci.getType());
    imageView.setImageResource(ci.getResource());

    最后看一下在横屏效果

    坚屏的效果,也没有问题:


  • 相关阅读:
    [AT2064] [agc005_f] Many Easy Problems
    [AT2304] [agc010_c] Cleaning
    [AT2172] [agc007_e] Shik and Travel
    [AT2148] [arc063_c] Integers on a Tree
    [AT2363] [agc012_c] Tautonym Puzzle
    未能从程序集“netstandard, Version=2.0.0.0......”中加载类型“...”。
    Android Studio设置国内镜像代理
    新建一个浏览器APP
    Android Studio在Make Project时下载Grandle特别慢
    用JS添加和删除class类名
  • 原文地址:https://www.cnblogs.com/sharecenter/p/5621016.html
Copyright © 2011-2022 走看看