Android的:使宽度和自定义视图,相等的高度 [英] Android: Making the width and the height of a custom view equal
本文介绍了Android的:使宽度和自定义视图,相等的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用XML自定义视图(CoordinateGridView)是这样的:
I am using a custom view (CoordinateGridView) in XML like this:
<com.android.catvehicle.CoordinateGridView
android:id="@+id/coordinateGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
我试图让CoordinateGridView的宽度以填充屏幕的整个宽度。高度应再搭配宽度 - 因此,在定制视图应该总是画成一个正方形
I am trying to get the width of the CoordinateGridView to fill the entire width of the screen. The height should then match the width -- therefore, the custom view should always be drawn as a square.
我如何做到这一点?
推荐答案
覆盖在 CoordinateGridView
的 onMeasure
函数:
public void onMeasure(int widthSpec, int heightSpec) {
super.onMeasure(widthSpec, heightSpec);
int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
setMeasuredDimension(size, size);
}
请参阅 https://github.com/chiuki/android-square-view 一个完整的例子。
See https://github.com/chiuki/android-square-view for a full example.
这篇关于Android的:使宽度和自定义视图,相等的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文