【问题标题】:add guidelines under a button and to its right side在按钮下方和右侧添加指南
【发布时间】:2021-06-29 12:11:13
【问题描述】:

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

现在,我无法在按钮下方和右侧放置基准线。

我希望按钮从右侧“边缘”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" />

它看起来像:

                         |
            button  4.1% | END_OF_SCREEN
             4.1%        |
             ____________
       BOTTOM_OF_SCREEN

【问题讨论】:

  • 你必须为 right -- start/end 指定指南方向,对于底部:方向将是水平的,方向将是底部,分离两条指南,一条不足以对齐右侧和底部

标签: android android-constraintlayout constraintlayout-guideline


【解决方案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"/>

【讨论】:

    【解决方案2】:

    另一个不使用指南的选项是使用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"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多