在标签之间切换后,Android ConstraintLayout有时无法正确绘制

时间:2017-08-05 20:07:05

标签: android android-fragments android-constraintlayout

我看到ConstraintLayout偶尔无法正确绘制,但只有当我在片段之间切换时,我才会看到原因。

最初看起来像这样: It should look like this

但是,在切换标签后,它看起来像这样: Sometimes it looks like this

fragment_overview.xml Fragment布局)

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/palette_grey_light"
    android:clipToPadding="false"
    android:paddingTop="16dp">

    <uk.lobsterdoodle.edinburghwolves.view.FixtureView
        android:id="@+id/overview_fixture_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    ... a few other views ...

</android.support.constraint.ConstraintLayout>

fixture_view.xml (自定义视图布局FixtureView

<?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:layout_width="match_parent"
    android:layout_height="wrap_content">

    <View
        android:id="@+id/fixture_away_color"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/palette_red"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/fixture_home_color"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <View
        android:id="@+id/fixture_home_color"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/palette_grey"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toEndOf="@+id/fixture_away_color"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/fixture_away_team_score"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="27"
        android:textColor="@color/palette_white"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="@+id/fixture_away_team_name"
        app:layout_constraintStart_toStartOf="@+id/fixture_away_team_name"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/fixture_away_team_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="32dp"
        android:text="WOLVES"
        android:textColor="@color/palette_white"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/fixture_home_color"
        app:layout_constraintTop_toBottomOf="@+id/fixture_away_team_score"
        app:layout_constraintVertical_bias="0.0"
        android:layout_marginTop="0dp"/>

    <TextView
        android:id="@+id/fixture_home_team_score"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="21"
        android:textColor="@color/palette_white"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="@+id/fixture_home_team_name"
        app:layout_constraintStart_toStartOf="@+id/fixture_home_team_name"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/fixture_home_team_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="32dp"
        android:text="PIRATES"
        android:textColor="@color/palette_white"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/fixture_home_color"
        android:layout_marginTop="0dp"
        app:layout_constraintTop_toBottomOf="@+id/fixture_home_team_score"/>

    <TextView
        android:id="@+id/fixture_state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="FINAL"
        android:textColor="@color/palette_white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/fixture_away_team_score"
        android:layout_marginBottom="0dp"
        />

</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

虽然您的fixture_view.xml文件正确约束了ConstraintLayout中的观看次数,但ConstraintLayout中的fragment_overview.xml中的视图不受限制,因此无法正确显示运行。在FixtureView fragment_overview.xml中为<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/palette_grey_light" android:clipToPadding="false" android:paddingTop="16dp"> <!-- You were missing constraints here --> <uk.lobsterdoodle.edinburghwolves.view.FixtureView android:id="@+id/overview_fixture_view" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginStart="16dp" android:layout_marginTop="16dp" app:cardBackgroundColor="@color/palette_white" app:cardElevation="2dp" app:contentPadding="16dp" app:layout_constraintTop_toBottomOf="@id/overview_fixture_view" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="standings" android:textColor="@color/palette_grey"/> </android.support.v7.widget.CardView> </android.support.constraint.ConstraintLayout> 添加一些约束,如下所示:

>>> from operator import itemgetter

>>> indices = [[1, 34, 56], [2, 54, 34], [2, 89, 173], [6, 23, 13]]

>>> x = list (map (itemgetter (1), indices))

>>> y = list (map (itemgetter (2), indices))

>>> map.scatter (*map (x, y))

答案 1 :(得分:0)

修正了将分数TextView的高度wrap_content设为0dp而不是匹配<TextView android:id="@+id/fixture_away_team_score" android:layout_width="wrap_content" android:layout_height="wrap_content" <-- wrap, not 0dp ... /> 的约束

inline-flex