ConstraintLayout如何打包以保持相同的距离比例

时间:2018-09-26 10:02:14

标签: android android-constraintlayout

应用所有要查找的边距后,我的布局超出了视图边界。我读到,应用链将在包装视图边界内的所有元素时保持相同的比率。

<?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">

<TextView
    android:id="@+id/Texto2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="dsadsadsadsadasd"
    app:layout_constraintTop_toBottomOf="@+id/texto1" />

<TextView
    android:id="@+id/texto1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="gggggg" />

<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/tut_first_img"
    app:layout_constraintTop_toBottomOf="@+id/Texto2" />

<Button
    android:id="@+id/botao"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ggggggggggggggggggggg"
    app:layout_constraintTop_toBottomOf="@+id/img" />

</android.support.constraint.ConstraintLayout>

这是在没有所有边距的情况下发生的情况,所有边距都将应用它超出视图屏幕。如何在保持比例的同时进行压缩?另外,如何将所有视图垂直对齐?

enter image description here

3 个答案:

答案 0 :(得分:1)

尝试一下:

<?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=".MainActivity">

<TextView
    android:id="@+id/Texto2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="dsadsadsadsadasd"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/texto1" />

<TextView
    android:id="@+id/texto1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="gggggg"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/img"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:src="@mipmap/ic_launcher"
    app:layout_constraintBottom_toTopOf="@+id/botao"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/Texto2" />

<Button
    android:id="@+id/botao"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="ggggggggggggggggggggg"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

只需复制粘贴一下,让我知道它是否有效。

答案 1 :(得分:1)

这是根据您的用例编写的代码,请。检查:

<?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="match_parent"
    android:background="#00ff00">

    <TextView
        android:id="@+id/Texto2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dsadsadsadsadasd"
        app:layout_constraintBottom_toTopOf="@+id/img"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/texto1" />

    <TextView
        android:id="@+id/texto1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="gggggg"
        app:layout_constraintBottom_toTopOf="@+id/Texto2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/common_full_open_on_phone"
        app:layout_constraintBottom_toTopOf="@+id/botao"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Texto2" />

    <Button
        android:id="@+id/botao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ggggggggggggggggggggg"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/img" />

</android.support.constraint.ConstraintLayout>

我希望它能对您有所帮助,期待从我的头开始是否需要任何进一步的帮助。

截屏:

enter image description here

答案 2 :(得分:1)

我希望下面提到的代码可以帮助您实现所需的布局。

<?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:background="@android:color/holo_blue_bright"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/Texto2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dsadsadsadsadasd"
        app:layout_constraintBottom_toTopOf="@id/texto1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="spread" />

    <TextView
        android:id="@+id/texto1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="gggggg"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/Texto2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/img" />

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/sym_def_app_icon"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/texto1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/botao" />

    <Button
        android:id="@+id/botao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ggggggggggggggggggggg"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/img"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

屏幕截图(由上述代码实现的用户界面):

enter image description here

如果您不熟悉ConstraintLayout,则可以使用链符号(最右边的符号;在布局中的第一个视图下方)来切换并应用不同的链样式(根据您的需要)。