安卓GridView的最后一列被截断

8
我在我的应用程序中有一个GridView,其中包含TextViews,但由于某种原因,最后一列被切断了(请参见下面的屏幕截图)。我猜测这与GridView左侧的一点间隙有关,但我不知道是什么原因导致的。
我做错了什么? screenshot 我将GridView的背景色调暗了一些,比片段背景更暗。
我的代码:

layout/fragment.xml:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                         xmlns:app="http://schemas.android.com/apk/res-auto"
                                         xmlns:tools="http://schemas.android.com/tools"
                                         android:layout_width="match_parent"
                                         android:layout_height="match_parent"
                                         android:background="#ADD178"
                                         tools:context="com.example.myapp.Fragment"
                                         android:id="@+id/constraintLayout">
<android.support.v7.widget.CardView
    android:id="@+id/current_card"
    android:layout_width="73dp"
    android:layout_height="89dp"
    app:cardBackgroundColor="#E917BC"
    app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
    android:layout_marginStart="16dp"
    app:layout_constraintTop_toTopOf="@+id/constraintLayout"
    android:layout_marginTop="16dp">

    <TextView
        android:id="@+id/current_card_letter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="40dp"
        android:gravity="center"
        android:textColor="#000000"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp"/>
    </android.support.v7.widget.CardView>

<GridView
    android:id="@+id/all_cards_grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="auto_fit"
    android:columnWidth="60dp"
    android:horizontalSpacing="5dp"
    android:verticalSpacing="5dp"
    android:stretchMode="spacingWidthUniform"
    android:background="#30000000"
    app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
    android:layout_marginStart="8dp"
    app:layout_constraintTop_toBottomOf="@+id/current_card"
    android:layout_marginTop="8dp"
    app:layout_constraintRight_toRightOf="@+id/constraintLayout"
    android:layout_marginEnd="8dp"
    app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
    android:layout_marginBottom="8dp"/>

</android.support.constraint.ConstraintLayout>

从适配器中获取视图:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    TextView textView;
    if (convertView == null) {
        textView = new TextView(mContext);
        textView.setPadding(CARD_PADDING, CARD_PADDING, CARD_PADDING, CARD_PADDING);
        textView.setLayoutParams(new GridView.LayoutParams(160, 160));
        textView.setTextSize(40);
        textView.setTextColor(Color.BLACK);
        textView.setGravity(Gravity.CENTER);
    } else {
        textView = (TextView) convertView;
    }

    textView.setBackgroundResource(getItem(position).getThumbnailID());
    textView.setText(getItem(position).getLetter());

    return textView;
}

当您将 android:columnWidth="60dp" 减小到更小的值时会发生什么? - Aleksandar Stefanović
你尝试过将 stretchMode 更改为 android:stretchMode="columnWidth" 吗? - Rehan
请尝试删除 android:horizontalSpacing="5dp"。我认为它会干扰 android:stretchMode="spacingWidthUniform" - Robbe
我尝试了,最后一列仍然被截断 :( - Tako
2
在GridView中删除layout_marginStart和layout_marginEnd,然后尝试。 - Naveen Kumar M
显示剩余3条评论
2个回答

2
您希望columnWidth为60dp,并使用auto_fitnum_columns属性,GridView通过此属性来测量列数。在您的适配器中,您强制设置了项目大小,这与GridView的宽度和列数不匹配。

以下是一种计算实际columnWidth的方法,以便适应。无法百分之百确定这里的数学是否正确,但希望这可以帮助您解决问题。

如果您想要项目成为矩形,则可以将计算出的宽度传递给适配器,并将其用于视图布局参数的宽度和高度。

活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final GridView gv = (GridView) findViewById(R.id.all_cards_grid_view);
    gv.post(new Runnable() {
        @Override
        public void run() {
            gv.setColumnWidth((gv.getWidth() - (gv.getNumColumns() * gv.getHorizontalSpacing())
                    - gv.getHorizontalSpacing() * 2) / gv.getNumColumns());
            gv.setAdapter(new TestAdapter(MainActivity.this));
        }
    });
}

public class TestAdapter extends BaseAdapter {

    private static final int CARD_PADDING = 10;

    private Context mContext;

    public TestAdapter(Context context) {
        mContext = context;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView textView;
        if (convertView == null) {
            textView = new TextView(mContext);
            textView.setPadding(CARD_PADDING, CARD_PADDING, CARD_PADDING, CARD_PADDING);
            //textView.setLayoutParams(new GridView.LayoutParams(200, 200));
            textView.setTextSize(40);
            textView.setTextColor(Color.BLACK);
            textView.setBackgroundColor(Color.BLUE);
            textView.setGravity(Gravity.CENTER);
        } else {
            textView = (TextView) convertView;
        }

        textView.setText("A");

        return textView;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public int getCount() {
        return 20;
    }
}

Xml:

<GridView
    android:id="@+id/all_cards_grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:background="#30000000"
    android:columnWidth="60dp"
    android:numColumns="auto_fit"
    android:horizontalSpacing="0dp"
    android:stretchMode="spacingWidthUniform"
    android:verticalSpacing="5dp"/>

当我尝试这个解决方案时,卡片会缩小一点,GridView会在行中添加一个更多的卡片,因此最后一列仍然被切割。如果我将numColumns参数更改为常数,则卡片之间的空格会增加,但是最后一行仍然被切割。 我期望的方式是列/卡片的宽度将被硬编码,卡片之间的空格将增长或缩小以适应不同的屏幕尺寸。 我相信我只是错过了有关GridView宽度的某些内容。 - Tako

1

移除android:layout_marginStart和android:layout_marginEnd属性,它们就不会被推到超出边界的位置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接