[英]Android: Making the width and the height of a custom view equal
我在XML中使用自定義視圖(CoordinateGridView),如下所示:
<com.android.catvehicle.CoordinateGridView
android:id="@+id/coordinateGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
我正在嘗試獲取CoordinateGridView的寬度以填充屏幕的整個寬度。 高度應與寬度匹配-因此,自定義視圖應始終繪制為正方形。
我該怎么做呢?
覆蓋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 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.