GridView getRequestedColumnWidth 与 getColumnWidth 区别

最近公司项目用到GridView,用到相关API时候,有这个疑问,网上找过也没什么明确答案。
官文 里面,看了对 getRequestedColumnWidth 的说明也是糊里糊涂的。

Return the requested width of a column in the grid.
This may not be the actual column width used. Use getColumnWidth() to retrieve the current real width of a column.

写了一个小demo试了试。


一、相关文件说明

1. MyGridView: 继承 GridView,重写了onMeasure,方便打印Log

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.d("MyLog", "getRequestedColumnWidth(px): " + getRequestedColumnWidth());
        Log.d("MyLog", "getRequestedColumnWidth(dp): " + Tools.pxToDp(getContext(), getRequestedColumnWidth()));
        Log.d("MyLog", "getColumnWidth(px): " + getColumnWidth());
        Log.d("MyLog", "getColumnWidth(dp): " + Tools.pxToDp(getContext(), getColumnWidth()));
    }

2.SquareLayout: 继承RelativeLayout,为保证Item是正方形

用到的是以下文件

https://github.com/goodev/SquareGridView/blob/master/SquareGrid/src/org/goodev/squaregrid/SquareLayout.java

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // For simple implementation, or internal size is always 0.
        // We depend on the container to specify the layout size of
        // our view. We can't really know what it is since we will be
        // adding and removing different arbitrary views and do not
        // want the layout to change as this happens.
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

        // Children are just made to fill our space.
        int childWidthSize = getMeasuredWidth();

        //高度和宽度一样
        heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

3.item.xml

<com.qiang.gridviewdemo.SquareLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#74c76f">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/image"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#57080404"
        android:gravity="center"
        android:padding="5dp"
        android:textColor="@android:color/white"
        android:textSize="14sp"
        tools:text="asdfasdfasdf"/>

</com.qiang.gridviewdemo.SquareLayout>

截图

4.activity_main.xml : 主要对MyGridView进行相关属性设置

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.qiang.gridviewdemo.MyGridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:horizontalSpacing="5dp"
        android:padding="5dp"
        android:scrollbars="none"
        android:verticalSpacing="5dp"/>

</LinearLayout>

5.MainActivity:

public class MainActivity extends AppCompatActivity {

    private MyGridView gridView;
    private List<User> mUsers;

    private static final int COLUMN_NUM = GridView.AUTO_FIT;
    private static final float COLUMN_WIDTH = 150f;
    private static final int STRETCH_MODE = GridView.STRETCH_COLUMN_WIDTH;
    private int mColumnWidth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridView = (MyGridView) findViewById(R.id.gridView);

        // 初始化List,加入30个User数据,有name属性
        initList();

        mColumnWidth = Tools.dpToPx(this, COLUMN_WIDTH);

        Log.d("MyLog", "mColumnWidth(px): " + mColumnWidth);

        gridView.setNumColumns(COLUMN_NUM);
        gridView.setColumnWidth(mColumnWidth);
        gridView.setStretchMode(STRETCH_MODE);

        gridView.setAdapter(new GridAdapter(this, mUsers));
    }
}

6.Adapter : 继承BaseAdapter,普通写法。

7.User : 普通实体类,有name 和 avatarUrl 两个属性。

8.Tools : 工具类,里面有 dp 与 px 互转的两个方法。

9.用到的测试机型: Cool dual1 , 应该属于 xxhdpi


二、实验开始

1. 正常启动App,默认是竖屏

(1) 截图

(2) Log

2. 旋转屏幕90° , 切换成横屏

(1) 截图

(2) Log


三、结合二里面的1、2总结

getRequestedColumnWidth : 获得的是GridView一开始设定好的ColumnWidth
getColumnWidth ** : 获得的是GridView经过自动调节过**的ColumnWidth


最后~ 安卓小菜鸟第一次写分享文章,虽然这只是个小小小的知识点。。。请大神绕道~
写得不好请大家多多指出。谢谢(应该没人看的~)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,284评论 25 708
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,541评论 2 45
  • 终于,主任还是问起了病例的整理情况。 我以为可以一拖再拖,主任会忙的忘记这件事,只是,我这如意小算盘打错了。 所以...
    文小辉cool阅读 193评论 0 0
  • 12月3日中午饭炒饭花53晚饭面花100水果7元
    郭郭郭58阅读 348评论 0 0
  • The set S originally contains numbers from 1 to n. But un...
    JRTx阅读 339评论 0 0