【问题标题】:Android ConstraintLayout shifts location of spinnerAndroid ConstraintLayout 移动微调器的位置
【发布时间】:2020-04-29 19:47:43
【问题描述】:

我正在尝试在布局底部使用微调器制作画廊,这会改变画廊的目录。不幸的是,微调器总是向上移动(或者 RecyclerView 约束可能向下移动?)。这是我的代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ProjectActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <Spinner
            android:id="@+id/spinnerProject"
            android:layout_width="141dp"
            android:layout_height="100dp" />
    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

结果如下:

知道有什么问题吗?

【问题讨论】:

  • 这里的问题是,当您单击微调器时,它会打开并显示微调器上方的数据。发生这种情况是因为,微调器数据显示在回收站视图的上方。
  • 问题是我希望 Spinner 与 RecyclerView 完全分离,而是重叠。我想这是我对“wrap_content”的理解不好。

标签: android android-layout android-recyclerview android-spinner android-constraintlayout


【解决方案1】:

对于这种类型的垂直线性布局,请始终尝试使用 ConstraintLayout 提供的垂直链接。 这是您的 XML 的更新代码。

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<Spinner
    android:id="@+id/spinnerProject"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    app:layout_constraintTop_toBottomOf="@+id/gallery"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/gallery"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/spinnerProject"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

【讨论】:

  • 是的,这符合我的预期。我想知道为什么如果我将 RecyclerView 高度更改为“wrap_content”,它又会搞砸了。无论如何感谢您的帮助!
  • @user13278676 如果你想设置recyclerview的高度wrap_content那么请必须使用constraint_vertical_bias。我只是通过将回收站视图高度设置为 0dp 将枪放在 ConstraintLayout 的肩膀上。
  • 我想我明白了!再次感谢你,你帮了我很多:)
猜你喜欢
  • 1970-01-01
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多