我正在 java Android Studio 中實作一個網格視圖,以將畫廊中的所有影像顯示到其中。一切正常,但我的網格視圖專案顯示得非常小。列之間有很多不必要的水平間距,這使得專案變小。這是我的具有網格視圖的活動 xml 檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".ImportFromGallery">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25">
</RelativeLayout>
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.003"
android:background="#e1e1e1" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.747">
<GridView
android:id="@ id/galleryViewGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:stretchMode="columnWidth">
</GridView>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.003"
android:background="#e1e1e1" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.097">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.8">
</RelativeLayout>
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.003"
android:background="#e1e1e1" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.197">
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
這是我要在網格視圖的每個專案中顯示的影像視圖:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dp">
<ImageView
android:id="@ id/gridGalleryImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
這是我得到的輸出:
-------------------------------------------------- -----------------------------------------
這是我想要的輸出:

這可能是什么問題。謝謝你。
uj5u.com熱心網友回復:
為您在網格視圖專案中膨脹的影像視圖使用以下代碼:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="3dp">
<ImageView
android:id="@ id/gridGalleryImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446619.html
