android如何设置自定义 LinearLayout
内部高度的一半。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec / 2);
}
我使用此代码,但是孩子的代码被剪掉了。
答案 0 :(得分: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);
}
}