With every view, android provide an override method(onMeasure) in which there are 2 parameters(int widthMeasureSpec, int heightMeasureSpec), You can return height width according to your requirement.
Example: If you want height = width means a square size view you can return height and width like this:
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(heightMeasureSpec, heightMeasureSpec);
}
You can create your custom view for this :
Example:
Make a class names CustomImageView extends with ImageView
public class CustomImageView extends ImageView
{
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(heightMeasureSpec, heightMeasureSpec);
// you can set this as per percentage also. For example: "heightMeasureSpec * 50 /100" ---- the height will be 50% of the view's width.
}
}
Use this in your layout like this :
<CustomImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
No comments:
Post a Comment