【问题标题】:Fragment constraint layout stretching on top of tabs of the main activity片段约束布局在主要活动的选项卡上伸展
【发布时间】:2018-12-02 00:55:43
【问题描述】:

我用ListView、空视图和总计数视图构建了一个带有约束布局的片段。底部约束正在起作用(尽管进行了人为调整),但顶部与我的 MainActivity 中的选项卡重叠:

我的代码在这里,这是我第一次使用约束布局:

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



    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/total_items_view"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.98"
        tools:layout_editor_absoluteX="8dp" />

    <TextView
        android:id="@+id/total_items_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/colorOrange"
        android:padding="4dp"
        android:text="Items in this category: "
        android:textSize="28sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintRight_toLeftOf="@id/items_number_view"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/items_number_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/colorOrange"
        android:padding="4dp"
        android:text="44"
        android:textSize="28sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintLeft_toRightOf="@id/total_items_view" />


    <RelativeLayout
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:visibility="gone">

        <ImageView
            android:id="@+id/empty_book_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/orange_orange_book" />

        <TextView
            android:id="@+id/empty_title_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/empty_book_image"
            android:layout_centerHorizontal="true"
            android:fontFamily="sans-serif-medium"
            android:paddingTop="16dp"
            android:text="This category is still empty..."
            android:textAppearance="?android:textAppearanceMedium" />

        <TextView
            android:id="@+id/empty_subtitle_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/empty_title_text"
            android:layout_centerHorizontal="true"
            android:fontFamily="sans-serif"
            android:paddingTop="8dp"
            android:text="Let's start with adding books!"
            android:textAppearance="?android:textAppearanceSmall"
            android:textColor="#A2AAB0" />
    </RelativeLayout>


</android.support.constraint.ConstraintLayout> 
}

有没有办法在单独的(主要)活动中为视图设置约束?我该如何解决这个问题?

我试过wrap_content,我唯一能想到的就是手动设置它有点离顶部,这破坏了约束布局的概念。

谢谢。

【问题讨论】:

  • 问题出在 ConstraintLayout 之外:ViewPager 和 TabLayout 混淆了。

标签: android constraints android-tabs android-constraintlayout


【解决方案1】:

您不能在约束布局之外应用约束。问题在于父布局,您的视图寻呼机似乎与选项卡布局重叠。

【讨论】:

  • 这是真的!约束布局与它无关 - 这是 ViewPager 和 TabsLayout 放置的愚蠢错误。 ConstraintLayout 似乎确实很摇滚:D
【解决方案2】:

首先,不要在 ConstraintLayout 中使用match_parent,例如。如果要使视图与屏幕宽度匹配,请使用

android:layout_width="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"

它重叠的原因是您可能不会将列表(或列表容器)的顶部限制在此顶部视图的底部,例如

app:layout_constraintTop_toBottomOf="@+id/top_view"

【讨论】:

    【解决方案3】:

    您的ListView's 高度设置为wrap_content,在这种情况下,它将根据需要扩展并与其他视图重叠。如果您想同时使用wrap_content 并强制执行视图的约束,则需要将app:layout_constrainedHeight="true" 属性添加到您的ListView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      相关资源
      最近更新 更多