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

    xiaoxiao2021-04-18  43

    最近碰上需求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);

    转载请注明原文地址: https://ju.6miu.com/read-674499.html

    最新回复(0)