0

我在整个屏幕上有一个图像视图,右下角有一个按钮。

现在,我没有成功在按钮下方和右侧放置基准线。

我希望按钮从右侧“边缘”为 4.1%,从底部“边缘”为 4.1%,使用指南。

这是我的尝试。

如何解决?

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageView" />


        <Button
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:id="@+id/button_try"
            app:layout_constraintRight_toLeftOf="@+id/guideline_for_try_from_bottom"
            app:layout_constraintBottom_toTopOf="@+id/guideline_for_try_from_bottom"/>

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline_for_try_from_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintWidth_max="26dp"
            app:layout_constraintWidth_min="15dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintGuide_percent="0.041" />

</androidx.constraintlayout.widget.ConstraintLayout>

它看起来像:

                         |
            button  4.1% | END_OF_SCREEN
             4.1%        |
             ____________
       BOTTOM_OF_SCREEN
4

2 回答 2

1

ConstraintLayout指南百分比展示位置是从屏幕的顶部和左侧测量的,因此对于从底部或底部开始的“4.1%”展示位置,您需要指定 1.0 - .041 或 0.956 作为百分比。

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.956"/>

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.956"/>
于 2021-06-29T12:38:47.703 回答
1

另一个不使用指南的选项是使用app:layout_constraintHorizontal_bias="0.956"app:layout_constraintVertical_bias="0.956"按钮,如下所示:

<Button
        android:id="@+id/button_try"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:insetLeft="0dp"
        android:insetTop="0dp"
        android:insetRight="0dp"
        android:insetBottom="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.956"
        app:layout_constraintVertical_bias="0.956"/>
于 2021-06-29T13:10:42.940 回答