以编程方式将ConstraintLayout添加到RelativeLayout时出现问题

时间:2017-12-08 08:29:40

标签: android android-layout android-constraintlayout

我刚开始使用ConstraintLayout,我已经使用ConstraintLayout创建了布局。这是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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintSetLay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">

    <TextView
        android:id="@+id/id_question_text"
        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="center"
        android:padding="10dp"
        android:text="@string/game_question_display"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/guideline4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.55" />

    <Button
        android:id="@+id/id_option_four"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="4dp"
        android:layout_marginTop="4dp"
        android:background="@color/colorPrimary"
        android:padding="@dimen/padding_10dp"
        android:text="@string/game_option_four"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/id_option_three"
        app:layout_constraintTop_toBottomOf="@+id/id_option_two" />

    <Button
        android:id="@+id/id_option_three"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="4dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="4dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:background="@color/colorPrimary"
        android:text="@string/game_option_three"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/id_option_four"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_option_one" />

    <Button
        android:id="@+id/id_option_one"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="4dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="4dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorPrimary"
        android:text="@string/game_option_one"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/id_option_three"
        app:layout_constraintEnd_toStartOf="@+id/id_option_two"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline4" />

    <Button
        android:id="@+id/id_option_two"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="4dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorPrimary"
        android:text="@string/game_option_two"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/id_option_four"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/id_option_one"
        app:layout_constraintTop_toBottomOf="@+id/guideline4" />
</android.support.constraint.ConstraintLayout>

我已夸大此视图,并尝试以编程方式在RelativeLayout中添加此视图。但是在添加了膨胀视图(ConstraintLayout)之后,缩小到屏幕顶部。请查看图片Here

我尝试使用ConstraintLayout代替RelativeLayout,但没有改变。

这是我正在使用的RelativeLayout代码。

<?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="match_parent"
    tools:context="com.radio.constraintlayoutapp.MainActivity">

    <RelativeLayout
        android:id="@+id/relativeTest"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="w,1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

以下是Github上代码的link

0 个答案:

没有答案