【发布时间】:2016-01-09 01:09:12
【问题描述】:
我无法让 GridView 在横向模式下正常工作。它的android:stretchMode="columnWidth" 在横向模式下似乎效果不佳。
人像模式
如您所见,我有 20x20 的方形网格,非常适合屏幕。 (虽然右边缘有瑕疵,但我现在可以忽略它)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/start_button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Random"
android:id="@+id/random_button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="@+id/clear_button" />
</LinearLayout>
<GridView
android:id="@+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="20"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:paddingLeft="2dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:stretchMode="columnWidth"
android:background="#ffc0c0c0"
android:scrollbars="none"
android:layout_gravity="center"
android:gravity="center" />
</LinearLayout>
横向模式
但是,20x20 方形网格不能很好地融入屏幕。我不希望网格视图可以滚动。我想在整个屏幕上显示整个 20x20 方格。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/start_button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Random"
android:id="@+id/random_button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="@+id/clear_button" />
</LinearLayout>
<GridView
android:id="@+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="20"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:paddingLeft="2dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:stretchMode="columnWidth"
android:background="#ffc0c0c0"
android:scrollbars="none"
android:layout_gravity="center"
android:gravity="center" />
</LinearLayout>
我可以知道我做错了什么,导致横向模式看起来不如纵向模式吗?
注意,为了使网格视图项看起来是方形的,我使用以下自定义类作为网格视图项。
public class SquareTextView extends TextView {
public SquareTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public SquareTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public SquareTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}
【问题讨论】: