我在我的应用程序中有一个GridView,其中包含TextViews,但由于某种原因,最后一列被切断了(请参见下面的屏幕截图)。我猜测这与GridView左侧的一点间隙有关,但我不知道是什么原因导致的。
我做错了什么?
我将GridView的背景色调暗了一些,比片段背景更暗。
我的代码:
我做错了什么?
![screenshot](https://istack.dev59.com/9ieFb.webp)
我的代码:
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"
吗? - Rehanandroid:horizontalSpacing="5dp"
。我认为它会干扰android:stretchMode="spacingWidthUniform"
。 - Robbe