在设备中制作应用 运行 时,ConstraintLayout 中的视图未按预期放置
A View in ConstraintLayout not being placed as expected when app is made run in device
我正在使用 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 的activity:
<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。它在编辑器中占据了一个合适的位置(在 parent 的末尾,有 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
的位置。
更改 你最后一个回收站视图布局的文本视图为此。我想问题就出在这里。
<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" />
或者您可以尝试将 Constraint 布局更改为任何其他布局,例如 LinearLayout 或 Relative。有时约束布局会导致回收器视图出现问题。
试试这个:
<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 的原因对齐,您只需要限制基线即可
看起来问题出在您的 RecyclerView
约束上。对于 documentation 中所述的 ConstraintLayout
中包含的 Views
,不建议使用 match_parent
。 RecyclerView
的正确水平约束应如下所示:
<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" />
尝试为您的 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" />
希望对您有所帮助。编码愉快。
问题出在其他地方。
在 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);
我不确定为什么会这样。如果有人可以解释,将不胜感激。
谢谢大家的回答!
我正在使用 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 的activity:
<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。它在编辑器中占据了一个合适的位置(在 parent 的末尾,有 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
的位置。
更改 你最后一个回收站视图布局的文本视图为此。我想问题就出在这里。
<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" />
或者您可以尝试将 Constraint 布局更改为任何其他布局,例如 LinearLayout 或 Relative。有时约束布局会导致回收器视图出现问题。
试试这个:
<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 的原因对齐,您只需要限制基线即可
看起来问题出在您的 RecyclerView
约束上。对于 documentation 中所述的 ConstraintLayout
中包含的 Views
,不建议使用 match_parent
。 RecyclerView
的正确水平约束应如下所示:
<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" />
尝试为您的 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" />
希望对您有所帮助。编码愉快。
问题出在其他地方。 在 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);
我不确定为什么会这样。如果有人可以解释,将不胜感激。 谢谢大家的回答!