Android RecyclerView中的ImageView不是“;重量“;水平对齐
这就是它在预览和设备上的外观 行布局和RecyclerView布局的代码是最基本的 行具有单个图像视图:Android RecyclerView中的ImageView不是“;重量“;水平对齐,android,android-layout,android-recyclerview,Android,Android Layout,Android Recyclerview,这就是它在预览和设备上的外观 行布局和RecyclerView布局的代码是最基本的 行具有单个图像视图: <androidx.constraintlayout.widget.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "
<androidx.constraintlayout.widget.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 = "175dp"
android:layout_height = "175dp"
android:layout_margin = "4dp">
<ImageView
android:id = "@+id/imageView"
android:layout_width = "0dp"
android:layout_height = "0dp"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toTopOf = "parent"
tools:ignore = "ContentDescription"
tools:srcCompat = "@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
RecyclerView(该片段中唯一的视图)是一个跨度为2的网格:
<androidx.recyclerview.widget.RecyclerView
android:id = "@+id/recyclerView"
android:layout_width = "0dp"
android:layout_height = "0dp"
app:layoutManager = "androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toTopOf = "parent"
app:spanCount = "2"
tools:listitem = "@layout/row_dir_observer_secondary" />
我不知道是怎么回事,尝试了所有与重力相关的布局设置,尝试了权重,但唯一有效的方法是让父项开始填充。您在适配器中使用了硬编码宽度,使用match\u parent,我想您已经使用了gridlayoutmanager,span=2,所以不需要在那里更改任何内容 您已经在适配器中使用了硬编码宽度,使用了match\u parent,我认为您已经使用了带有span=2的gridlayoutmanager,所以不需要在那里更改任何内容 当您在RecyclerView中使用
GridLayoutManager
作为LayoutManager时,不要设置固定大小的宽度,因为如果您希望RecyclerView的每个项目都具有相同的宽度,那么如果您使用宽度作为匹配项,GridLayoutManager会根据SpanCout
自动划分屏幕宽度
所以首先从适配器的布局文件中删除硬编码的宽度值(这里的示例是175dp),并将其设置为匹配Adpater布局文件中的父宽度
试试这个
<androidx.constraintlayout.widget.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 = "175dp"
android:layout_margin = "4dp">
<ImageView
android:id = "@+id/imageView"
android:layout_width = "0dp"
android:layout_height = "0dp"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toTopOf = "parent"
tools:ignore = "ContentDescription"
tools:srcCompat = "@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
当您在RecyclerView中使用
GridLayoutManager
作为LayoutManager时,不要设置固定大小的宽度,因为如果您希望RecyclerView的每个项目都具有相同的宽度,那么GridLayoutManager会根据SpanCout
自动划分屏幕宽度,如果您使用宽度作为匹配项
所以首先从适配器的布局文件中删除硬编码的宽度值(这里的示例是175dp),并将其设置为匹配Adpater布局文件中的父宽度
试试这个
<androidx.constraintlayout.widget.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 = "175dp"
android:layout_margin = "4dp">
<ImageView
android:id = "@+id/imageView"
android:layout_width = "0dp"
android:layout_height = "0dp"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toTopOf = "parent"
tools:ignore = "ContentDescription"
tools:srcCompat = "@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
事后看来,这类事情总是显而易见的。。。谢谢很高兴知道它有帮助,高兴地编码:)这种东西在事后看来总是显而易见的。。。谢谢很高兴知道它有帮助,高兴地编码:)