【问题标题】:How to make GridView works well in landscape mode如何让 GridView 在横向模式下正常工作
【发布时间】: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);
    }
}

完整的源码可以看https://github.com/yccheok/GameOfLife

【问题讨论】:

    标签: android gridview


    【解决方案1】:

    我遇到了同样的问题,并通过扩展 GridView(没有 android:stretchMode)解决了它。在我自己的 GridView 中,我将以下代码放在 onMeasure 中:

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            if (getContext().getResources().getConfiguration().orientation == 1)
                super.onMeasure(widthMeasureSpec, widthMeasureSpec);
            else {
                int myWidth = ((View)getParent().getParent()).getHeight();
                if (myWidth > 0) {
                    View parent = (View)getParent();
                    myWidth -= parent.getPaddingBottom() + parent.getPaddingTop();
                }
                int measure = MeasureSpec.makeMeasureSpec(myWidth, MeasureSpec.EXACTLY);
                super.onMeasure(measure, measure);
            }
        }
    

    诀窍是获取 myWidth 的最顶层视图并将所有填充添加到 myWidth

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多