Created
May 29, 2013 13:46
-
-
Save kitek/5670362 to your computer and use it in GitHub Desktop.
Example of square gridview items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<com.your.app.SquareView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" > | |
<ImageView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/image" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:adjustViewBounds="true" | |
android:contentDescription="@string/descr_image" | |
android:scaleType="centerCrop" /> | |
</com.your.app.SquareView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pl.garnek; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.view.ViewGroup; | |
public class SquareView extends ViewGroup { | |
public SquareView(Context context) { | |
super(context); | |
} | |
public SquareView(final Context context, final AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public SquareView(final Context context, final AttributeSet attrs, final int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
@Override | |
protected void onLayout(boolean changed, int l, int u, int r, int d) { | |
getChildAt(0).layout(0, 0, r - l, d - u); // Layout with max size | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
View child = getChildAt(0); | |
child.measure(widthMeasureSpec, widthMeasureSpec); | |
int width = resolveSize(child.getMeasuredWidth(), widthMeasureSpec); | |
child.measure(width, width); // 2nd pass with the correct size | |
setMeasuredDimension(width, width); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i'm stilll confused , could you please make a full example? thanks