约束布局的未知行为

时间:2018-04-10 15:59:55

标签: android android-constraintlayout

我正在尝试使用约束布局创建一个简单的UI。在布局编辑器中UI显得很好但是当我在设备上运行时整个UI都搞砸了。 我认为我的代码中有关于约束的问题。任何帮助将不胜感激。

布局编辑器中的简单布局:

This is what i want

我得到的输出:

enter image description here

layout.xml:

 <?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"
    android:layout_height="wrap_content"
    android:padding="@dimen/padding_8dp">

    <ImageView
        android:id="@+id/imgRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/round"
        android:backgroundTint="@color/gray_light"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/txtTitleRow"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:paddingStart="@dimen/padding_16dp"
        android:text="@string/view_name"
        app:layout_constraintEnd_toStartOf="@+id/imgArrow"
        app:layout_constraintStart_toEndOf="@+id/imgRow"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/txtSubtitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:paddingStart="@dimen/padding_16dp"
        android:text="@string/view_subtitle"
        app:layout_constraintEnd_toStartOf="@+id/imgArrow"
        app:layout_constraintStart_toEndOf="@+id/imgRow"
        app:layout_constraintTop_toBottomOf="@+id/txtTitleRow" />

    <ImageView
        android:id="@+id/imgArrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_4dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_arrow_right" />

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:1)

只需使用RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:padding="8dp">

    <ImageView
        android:id="@+id/imgRow"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentStart="true"
        android:background="?android:attr/selectableItemBackground"
        android:backgroundTint="@color/text_gray"
        android:scaleType="fitCenter"
        android:src="@drawable/your_image"
        android:tint="@color/text_gray" />

    <TextView
        android:id="@+id/txtTitleRow"
        style="@style/TextAppearance.AppCompat.Medium"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:paddingStart="16dp"
        android:text="name"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imgRow"
        android:layout_toLeftOf="@+id/imgArrow"/>

    <TextView
        android:id="@+id/txtSubtitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:paddingStart="16dp"
        android:text="subtitle"
        android:layout_below="@+id/txtTitleRow"
        android:layout_toRightOf="@+id/imgRow"
        android:layout_toLeftOf="@+id/imgArrow"/>

    <!-- Remaining Note:Set border of button like playstore button-->

    <ImageView
        android:id="@+id/imgArrow"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:padding="4dp"
        android:scaleType="fitCenter"
        android:src="@drawable/your_image"
        android:tint="@color/text_black"
        android:layout_alignParentEnd="true"/>

</RelativeLayout>