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);
解决方案
我认为你的问题在这里:
app:layout_constraintBottom_toBottomOf="@+id/textViewReason"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/textViewReason"
您尝试使用的位置constraintTop
和constraintBottom
相同的视图。