我正在研究Constraintlayout我可以调整子视图,但是我无法将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像这样对齐。有没有办法呢? 像这样对齐?
答案 0 :(得分:2)
您需要从所有视图中删除app:layout_constraintStart_toStartOf="parent"
,除了第一个视图。因为它会在左侧分配您的所有视图。
<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" />
答案 1 :(得分:0)
从除第一个视图之外的所有视图中删除app:layout_constraintStart_toStartOf="parent"
,并为除第一个视图之外的每个视图添加app:layout_constraintStart_toEndOf = "@+id/your_left_views_id"
。
像
<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:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="left"
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: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="50"
android:textColor="@color/black"
android:textSize="@dimen/list_itemtextsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toEndOf="@+id/serialnumber"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/sellername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
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_toEndOf="@+id/lotnumber"
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:layout_marginTop="8dp"
android:background="@color/colorPrimary"
android:gravity="left"
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_constraintHorizontal_bias="0.66"
app:layout_constraintStart_toEndOf="@+id/sellername"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/createddate"
android:layout_width="0dp"
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_toEndOf="@+id/lotstatus"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
答案 2 :(得分:0)
您正在赞美所有观点 应用程式:layout_constraintStart_toStartOf =&#34;父&#34; 应用程式:layout_constraintEnd_toEndOf =&#34;父&#34;这是错的。
在约束布局中,您必须为相关视图提供开始,结束,顶部,底部约束。
尝试以下代码:
<?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:id="@+id/mylotslist_itemview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/serialnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:padding="@dimen/padding5"
android:text="1"
android:textColor="@color/color_btn_blue"
android:textSize="@dimen/list_itemtextsize"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/lotnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:padding="@dimen/padding5"
android:text="50"
android:textColor="@color/color_charcoal"
android:textSize="@dimen/list_itemtextsize"
app:layout_constraintStart_toEndOf="@+id/serialnumber"
app:layout_constraintTop_toTopOf="@+id/serialnumber" />
<TextView
android:id="@+id/sellername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:padding="@dimen/padding5"
android:text="ffffffffff"
android:textColor="@color/radius_text_blue"
app:layout_constraintStart_toEndOf="@+id/lotnumber"
app:layout_constraintTop_toTopOf="@+id/serialnumber" />
<TextView
android:id="@+id/lotstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/blue_text"
android:padding="@dimen/padding5"
android:text="Alloted and Empty"
android:textColor="@color/default_text"
android:textSize="@dimen/list_itemtextsize"
app:layout_constraintStart_toEndOf="@+id/sellername"
app:layout_constraintTop_toTopOf="@+id/serialnumber" />
<TextView
android:id="@+id/createddate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:background="@color/file_limit_error"
android:padding="@dimen/padding5"
android:text="01-FEB-2018 10:30 AM"
android:textColor="@color/default_text"
android:textSize="@dimen/list_itemtextsize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/lotstatus"
app:layout_constraintTop_toTopOf="@+id/serialnumber" />
</android.support.constraint.ConstraintLayout>