0

android how to set custom LinearLayout height half of width Inside herself.

 @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, widthMeasureSpec / 2);
}

i use this code but child's are cut off.

Ali Ahmed
  • 2,130
  • 1
  • 13
  • 19
AlirezaEs
  • 21
  • 4

1 Answers1

1
public class CustomLinearLayout extends LinearLayout {

    private static final float RATIO = 0.5f;

    public CustomLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        int customHeight = MeasureSpec.makeMeasureSpec(
                            (int)(MeasureSpec.getSize(widthSpec) * RATIO), MeasureSpec.EXACTLY);
        super.onMeasure(widthSpec, customHeight);
    }
}
ashakirov
  • 12,112
  • 6
  • 40
  • 40