我正在ConstraintLayout
上工作,可以调整 subview ,但是无法从左到右对齐Textview
。有什么办法可以从左到右对齐Textview
?
这是代码:
<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:id="@+id/mylotslist_itemview"
android:layout_height="wrap_content">
<TextView
android:id="@+id/serialnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:gravity="left"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:padding="@dimen/padding5"
android:text="1"
android:textColor="@color/black"
android:textSize="@dimen/list_itemtextsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/lotnumber"
android:gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:padding="@dimen/padding5"
android:text="50"
android:textColor="@color/black"
android:textSize="@dimen/list_itemtextsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/sellername"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="right"
android:padding="@dimen/padding5"
android:text="ffffffffff"
android:textColor="@color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
/>
<TextView
android:id="@+id/lotstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:gravity="left"
android:layout_marginTop="8dp"
android:background="@color/lot_statusfalse"
android:paddingBottom="@dimen/padding5"
android:paddingLeft="@dimen/padding5"
android:paddingRight="@dimen/padding5"
android:paddingTop="@dimen/padding5"
android:text="Alloted and Empty"
android:textColor="@color/white"
android:textSize="@dimen/list_itemtextsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.66"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/createddate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="left"
android:padding="@dimen/padding5"
android:text="01-FEB-2018 10:30 AM"
android:textColor="@color/black"
android:textSize="@dimen/list_itemtextsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
我希望我的textview像这样对齐。有什么办法可以
像这样对齐?
最佳答案
您需要从除第一个 View 之外的所有 View 中删除app:layout_constraintStart_toStartOf="parent"
,因为它将在左侧分配所有 View 。
<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:id="@+id/mylotslist_itemview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/serialnumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="8dp"
android:text="1"
android:textColor="@color/black"
android:textSize="@dimen/font_normal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.1" />
<TextView
android:id="@+id/lotnumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="8dp"
android:text="50"
android:textColor="@color/black"
android:textSize="@dimen/font_normal"
app:layout_constraintStart_toEndOf="@+id/serialnumber"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.1" />
<TextView
android:id="@+id/sellername"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="end"
android:padding="8dp"
android:text="ffffffffff"
android:textColor="@color/black"
app:layout_constraintStart_toEndOf="@+id/lotnumber"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.2" />
<TextView
android:id="@+id/lotstatus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:gravity="start"
android:padding="8dp"
android:text="Alloted and Empty"
android:textColor="@color/white"
android:textSize="@dimen/font_normal"
app:layout_constraintStart_toEndOf="@+id/sellername"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3" />
<TextView
android:id="@+id/createddate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="start"
android:padding="8dp"
android:text="01-FEB-2018 10:30 AM"
android:textColor="@color/black"
android:textSize="@dimen/font_normal"
app:layout_constraintStart_toEndOf="@+id/lotstatus"
app:layout_constraintWidth_percent="0.3" />
关于android - 如何在ConstraintLayout中为 subview 赋予重力,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50754334/