我想要一个正方形(宽度与高度相同)GridView以横向方向填充屏幕的整个高度。在GridView是(由8个方块8)棋盘与XML:在android中的全高方形元素
<com.example.jens.jchess2.view.MyGridView
android:id="@+id/chessboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:numColumns="8"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp">
</com.example.jens.jchess2.view.MyGridView>
和网格的元素是:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/square"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000080"
android:orientation="vertical"
android:padding="0pt">
<ImageView
android:id="@+id/square_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:padding="0pt" />
<ImageView
android:id="@+id/piece"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:padding="0pt" />
</FrameLayout>
,其中ImageViews对应于正方形和片(均来自PNG图像)的董事会。
在定制MyGridView我重写onMeasure如下:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = getContext().getResources().getDisplayMetrics().heightPixels;
if (width > height) {
super.onMeasure(
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
);
} else {
super.onMeasure(
MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY)
);
}
}
这使我在纵向和横向的方形GridView控件。在纵向模式下,它填满整个宽度,一切都很好。但在横向模式下,它会延伸到屏幕下方,因为GridView /板的高度(=宽度)太大。它由工具栏的高度和状态栏的高度过大。我怎样才能得到GridView的正确大小,即屏幕高度减去状态栏高度减去工具栏高度?
make heightMeasureSpec等于窗口的高度.... –