使应用在设备中运行时,ConstraintLayout中的视图未按预期放置

时间:2018-08-13 11:50:05

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

我正在使用ConstraintLayout创建将在RecyclerView中使用的视图。 由于某种原因,当我在设备中安装应用程序时,一个TextView发生了意外的情况。 下面是RecyclerView中使用的视图

<?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="wrap_content">

    <TextView
        android:id="@+id/textViewDate"
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="31-Jan"
        android:textSize="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textViewTime"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="10:10 PM"
        android:textSize="12dp"
        app:layout_constraintEnd_toEndOf="@+id/textViewDate"
        app:layout_constraintStart_toStartOf="@+id/textViewDate"
        app:layout_constraintTop_toBottomOf="@+id/textViewDate" />

    <TextView
        android:id="@+id/textViewReason"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="Reason"
        app:layout_constraintBottom_toBottomOf="@+id/textViewTime"
        app:layout_constraintStart_toEndOf="@+id/textViewDate"
        app:layout_constraintTop_toTopOf="@+id/textViewDate" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="@+id/textViewReason"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/textViewReason" />

</android.support.constraint.ConstraintLayout>

下面是包含RecyclerView的活动:

<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"
    tools:context=".activity.HomeActivity">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_action_name" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewTransactionList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

有问题的视图是textView2。它在编辑器中处于适当位置(在8dp填充的父级末尾)。但是当我安装apk时,它向左移动。 另外,我使用下面的书面代码填充RecyclerView:

TransactionRecyclerViewAdapter transactionRecyclerViewAdapter = new TransactionRecyclerViewAdapter(transactionList);
    LinearLayoutManager l = new LinearLayoutManager(getBaseContext());
    recyclerViewTransactionList.setAdapter(transactionRecyclerViewAdapter);
    l.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerViewTransactionList.setLayoutManager(l);

以下是编辑器和设备的屏幕截图: The editor image

This is rendered image

6 个答案:

答案 0 :(得分:0)

我认为您的问题出在这里

app:layout_constraintBottom_toBottomOf="@+id/textViewReason"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/textViewReason"

您要在同一视图中使用constraintTopconstraintBottom的地方。

答案 1 :(得分:0)

更改,您的回收者视图布局的上一个文本视图为此。我想问题就在这里。

 <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

或者您可以尝试将“约束”布局更改为任何其他布局,例如LinearLayout或Relative。有时约束布局会使回收者视图出现问题。

答案 2 :(得分:0)

尝试一下:

<TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBaseline_toBaselineOf="@id/textViewReason" />

理想情况下,如果您希望基线与textView原因对齐,则只需对其进行约束

答案 3 :(得分:0)

您的RecyclerView约束似乎是问题所在。如documentation所述,不建议对match_parent中包含的Views使用ConstraintLayoutRecyclerView的适当水平约束应如下所示:

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerViewTransactionList"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

答案 4 :(得分:0)

尝试将以下提到的代码用于RecyclerView:

(就像使用ConstraintLayout时,不得使用“ match_parent”,而应使用“ match_constraint”,其值等于“ 0dp”)。

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewTransactionList"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

此外,在用于RecyclerView的视图中尝试为您的“ textView2”使用以下代码:

<TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="TextView"
        app:layout_constraintStart_toEndOf="@id/textViewReason"
        app:layout_constraintTop_toTopOf="@+id/textViewReason"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/textViewReason"
        app:layout_constraintHorizontal_bias="1.0" />

希望这对您有所帮助。编码愉快。

答案 5 :(得分:0)

问题出在其他地方。 在onCreateViewHolder方法中,在扩大布局时,我没有向inflate()方法发送适当的参数:

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_oneday_expanse_view, null);

当我将传递的参数更改为这些参数时,问题得到解决:

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_oneday_expanse_view,parent, false);

我不确定为什么会这样。如果有人可以解释将不胜感激。 谢谢大家的回答!