最近公司項(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是正方形
用到的是以下文件
@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)該沒人看的~)