【问题标题】:Using ListView inside ConstraintLayout在 ConstraintLayout 中使用 ListView
【发布时间】:2018-03-26 10:25:35
【问题描述】:

我在Android Developers 上读到,ConstraintLayout 可用于为应用程序设计响应式布局。有一个父 ConstraintLayout,其中包含一个工具栏和两个其他 ConstraintLayout。第一个子 ConstraintLayout 将充当我的 ListView 的空视图。第二个 ConstraintLayout 包含我的列表视图和一个浮动操作按钮。目前,列表视图出现在工具栏下方,而不是下方。同样如屏幕截图所示,浮动操作按钮出现在可见区域之外。
请看下面的截图:

这是列表为空时的应用布局:

这是我的布局代码:

<?xml version="1.0" encoding="utf-8"?>


<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>


    <android.support.constraint.ConstraintLayout
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="?attr/actionBarSize">

        <TextView
            app:layout_constraintTop_toTopOf="parent"
            android:id="@+id/tv_empty_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/list_is_empty"
            android:textSize="@dimen/large_text"
            android:textStyle="bold"
            />

        <ImageView
            android:id="@+id/iv_empty_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/tv_empty_view"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:contentDescription="@string/list_is_empty"
            android:src="@drawable/emptybox" />
    </android.support.constraint.ConstraintLayout>
    <android.support.constraint.ConstraintLayout
        android:id="@+id/main_area"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="?actionBarSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintBottom_toBottomOf="parent">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="?attr/actionBarSize"
        android:layout_marginLeft="@dimen/margin_side"
        android:layout_marginStart="@dimen/margin_side"
        android:layout_marginRight="@dimen/margin_side"
        android:layout_marginEnd="@dimen/margin_side"
        android:layout_marginTop="?attr/actionBarSize"
        />
    <android.support.design.widget.FloatingActionButton
        android:layout_below="@+id/listview"
        android:id="@+id/fab"
        android:layout_width="@android:dimen/notification_large_icon_width"
        android:layout_height="@android:dimen/notification_large_icon_height"
        android:layout_gravity="end|bottom"
        android:src="@drawable/plus"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />
    </android.support.constraint.ConstraintLayout>



</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 您没有将constraints 添加到两个ConstraintLayouts

标签: android listview android-constraintlayout


【解决方案1】:

使用ConstraintLayout 时,您必须为'constraintTop'、constraintRightconstraintBottomconstraintLeftconstraintStartconstraintEnd 添加约束。仅当您约束所有四个边时,约束布局(或约束开始或结束与其他参考)才能正常工作。否则布局将无法正常工作

供进一步参考https://codelabs.developers.google.com/codelabs/constraint-layout/index.html?index=..%2F..%2Findex#0

https://developer.android.com/training/constraint-layout/index.html

【讨论】:

    【解决方案2】:

    您可以使用 XML 改进一些事情并使设计更容易。

    首先,主布局将匹配屏幕,为了让预览正确模拟,您可以将其宽度/高度设置为 match_parent

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    

    那么,Android Studio 应该会向您发出警告/错误并说The view is not constrained horizontally/vertically。在ConstraintLayout 中,您必须使用约束来指定视图的放置方式。如果你不这样做,默认情况下它们将定位在 0/0 并且很可能在设备上运行时看起来会有所不同:

    <ListView
            android:id="@+id/listview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="0dp" />
    

    现在,您应该能够使 main_area 与工具栏重叠。要修复它,您可以更改 main_area 高度以匹配约束:

    android:id="@+id/main_area"
    android:layout_height="0dp"
    

    您应该能够获得与您的预期相似的设计。

    【讨论】:

      【解决方案3】:

      这是您想要的结果,我做了一些更改,例如边距、src 只是为了让它在我的工作室中工作,所以您必须选择您使用的任何内容,只需将我的替换为您的 src 和边距等。 ..

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.ConstraintLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:id="@+id/content"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       >
      
       <android.support.v7.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_width="0dp"
          android:layout_height="56dp"
          android:background="?attr/colorPrimary"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintLeft_toLeftOf="parent"/>
      
      
      <android.support.constraint.ConstraintLayout
          app:layout_constraintTop_toBottomOf="@+id/toolbar"
          android:id="@+id/empty_view"
          android:layout_width="0dp"
          android:layout_height="0dp"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent">
      
          <TextView
              app:layout_constraintTop_toTopOf="parent"
              android:id="@+id/tv_empty_view"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              app:layout_constraintBottom_toBottomOf="parent"
              android:text="list_is_empty"
              android:textSize="30sp"
              android:textStyle="bold"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent" />
      
          <ImageView
              android:id="@+id/iv_empty_view"
              android:layout_width="20dp"
              android:layout_height="20dp"
              app:layout_constraintTop_toBottomOf="@+id/tv_empty_view"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              android:background="#fff222" />
      </android.support.constraint.ConstraintLayout>
      <android.support.constraint.ConstraintLayout
          android:id="@+id/main_area"
      
          android:layout_width="0dp"
          android:layout_height="0dp"
          app:layout_constraintTop_toBottomOf="@+id/toolbar"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent">
      
          <ListView
              android:id="@+id/listview"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent" />
          <android.support.design.widget.FloatingActionButton
              android:layout_marginEnd="30dp"
              android:layout_marginBottom="30dp"
              android:id="@+id/fab"
              android:layout_width="64dp"
              android:layout_height="64dp"
              android:src="@drawable/bg"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              />
      </android.support.constraint.ConstraintLayout>
      </android.support.constraint.ConstraintLayout>
      

      这是输出:

      【讨论】:

        【解决方案4】:

        我也遇到了同样的问题。

        1. 我错误地将layout_widthListView 保持为固定宽度。我将其更改为match_constraint,它可以正常运行,没有任何删减。
        2. 对于身高,我遇到了类似的问题,因此我将其更改为 match_constraint,它确实有效。

        【讨论】:

          猜你喜欢
          • 2020-07-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-01
          • 2023-03-24
          • 2018-03-21
          • 1970-01-01
          相关资源
          最近更新 更多