如何在ListView的列表行中添加两个不重叠的并排按钮?
Hey there! I get it—having your two buttons overlap instead of sitting side-by-side is super frustrating, especially when you’ve got 100 of these rows to render. Let’s break down the common issues with your ConstraintLayout setup and fix this step by step.
Common Causes of Overlap in ConstraintLayout
Most often, this happens because:
- Buttons are set to
match_parentfor width, so they’re fighting for the same space - Missing or incorrect constraints that don’t define how the buttons should sit relative to each other and the parent layout
- The root layout of your list item isn’t set to fill the available width
Fixed Custom Layout Example
Here’s a corrected version of your custom_layout.xml that ensures the two buttons sit side-by-side properly. I’ll fill in the missing parts of your original code with proper constraints:
<?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" <!-- Critical: make sure root fills width --> android:layout_height="wrap_content" android:padding="8dp"> <!-- Number Button --> <Button android:id="@+id/btn_number" android:layout_width="0dp" <!-- Use 0dp to let constraints define width --> android:layout_height="wrap_content" android:text="1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/btn_info" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_weight="1" <!-- Adjust for proportional spacing --> android:layout_marginEnd="8dp"/> <!-- Info (i) Button --> <Button android:id="@+id/btn_info" android:layout_width="wrap_content" <!-- Wrap content for the small i button --> android:layout_height="wrap_content" android:text="i" app:layout_constraintLeft_toRightOf="@+id/btn_number" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> </android.support.constraint.ConstraintLayout>
Key Fixes Explained
- Root Layout Width: Set to
match_parentso the row fills the entire screen width, giving your buttons room to sit side-by-side. - Number Button Width: Using
0dp(match_constraint) with left/right constraints tells the button to take up all available space except what’s reserved for the info button. Thehorizontal_weightensures it scales properly if you want proportional spacing. - Info Button Constraints: Tied to the right side of the number button and the right edge of the parent, so it stays pinned next to the number button without overlapping.
- Margins: Added a small margin between the buttons to prevent them from touching.
For Your 100-Item List
When setting up your RecyclerView Adapter, just make sure your adapter binds data to each row correctly—no extra steps needed for the layout once it’s fixed. As long as your ViewHolder inflates this corrected layout, all 100 rows will render the buttons side-by-side as expected.
If you were using a different layout manager (like LinearLayoutManager), double-check it’s set to vertical orientation (the default) so rows stack correctly.
内容的提问来源于stack exchange,提问作者Dijital Dünya

