牛骨文教育服务平台(让学习变的简单)
博文笔记

GridView子item正方形充满屏幕简单实现

创建时间:2017-04-14 投稿人: 浏览次数:885

最近碰上需求Gridview子项目平分屏幕宽度,子项目为正方形。简单实现如下:

假设需要一行3个子item,显示为正方形:

<GridView
    android:id="@+id/g1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="3"
    ></GridView>


private int a1 = 0;

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    a1 = getWindowManager().getDefaultDisplay().getWidth() / 3;

}

<LinearLayout
    android:id="@+id/r1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="5dp">

    <ImageView
        android:id="@+id/i1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        />
</LinearLayout>


mViewHolder.mRelativeLayout = (LinearLayout) convertView.findViewById(R.id.r1);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(a1, a1);
mViewHolder.mRelativeLayout.setLayoutParams(layoutParams);
convertView.setTag(mViewHolder);



声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。