GridView getRequestedColumnWidth 與 getColumnWidth 區(qū)別

最近公司項(xiàng)目用到GridView,用到相關(guān)API時(shí)候,有這個(gè)疑問,網(wǎng)上找過也沒什么明確答案。
官文 里面,看了對(duì) 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.

寫了一個(gè)小demo試了試。


一、相關(guān)文件說明

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 : 主要對(duì)MyGridView進(jìn)行相關(guān)屬性設(shè)置

<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個(gè)User數(shù)據(jù),有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 : 普通實(shí)體類,有name 和 avatarUrl 兩個(gè)屬性。

8.Tools : 工具類,里面有 dp 與 px 互轉(zhuǎn)的兩個(gè)方法。

9.用到的測(cè)試機(jī)型: Cool dual1 , 應(yīng)該屬于 xxhdpi


二、實(shí)驗(yàn)開始

1. 正常啟動(dòng)App,默認(rèn)是豎屏

(1) 截圖

(2) Log

2. 旋轉(zhuǎn)屏幕90° , 切換成橫屏

(1) 截圖

(2) Log


三、結(jié)合二里面的1、2總結(jié)

getRequestedColumnWidth : 獲得的是GridView一開始設(shè)定好的ColumnWidth
getColumnWidth ** : 獲得的是GridView經(jīng)過自動(dòng)調(diào)節(jié)過**的ColumnWidth


最后~ 安卓小菜鳥第一次寫分享文章,雖然這只是個(gè)小小小的知識(shí)點(diǎn)。。。請(qǐng)大神繞道~
寫得不好請(qǐng)大家多多指出。謝謝(應(yīng)該沒人看的~)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,191評(píng)論 25 708
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,891評(píng)論 2 45
  • 終于,主任還是問起了病例的整理情況。 我以為可以一拖再拖,主任會(huì)忙的忘記這件事,只是,我這如意小算盤打錯(cuò)了。 所以...
    文小輝cool閱讀 213評(píng)論 0 0
  • 12月3日中午飯炒飯花53晚飯面花100水果7元
    郭郭郭58閱讀 392評(píng)論 0 0
  • The set S originally contains numbers from 1 to n. But un...
    JRTx閱讀 363評(píng)論 0 0

友情鏈接更多精彩內(nèi)容