android - 约束布局指南

标签 android android-layout android-constraintlayout

我正在尝试使用约束布局指南设置文本 block 的左右填充。我想要在两侧使用以下代码进行 11% 的填充。但是,当我尝试设置填充时,它没有反射(reflect)出来。当我只使用左侧或右侧时,它会显示,但在 TextView 上同时使用左侧和右侧准则时,填充不会反射(reflect)出来。

这是代码

            <android.support.constraint.ConstraintLayout
                android:id="@+id/contentLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/centredIconLayout"
                android:layout_centerHorizontal="true"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:fontFamily="@font/gothamggm_bold"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="25dp"
                    android:text="@string/title_long"
                    android:textSize="17sp"
                    android:lineSpacingExtra="9sp"
                    android:textColor="@android:color/black"
                    app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
                    app:layout_constraintRight_toLeftOf="@+id/right_guideline"
                    />

                <TextView
                    android:id="@+id/body"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_marginTop="13dp"
                    android:fontFamily="@font/gothamssm_book"
                    android:lineSpacingExtra="9sp"
                    android:text="@string/content_large"
                    android:textColor="@color/body_grey"
                    android:textSize="12sp"
                    app:layout_constraintTop_toBottomOf="@+id/title"
                    app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
                    app:layout_constraintRight_toLeftOf="@+id/right_guideline"
                    />

                <android.support.constraint.Guideline
                    android:id="@+id/left_guideline"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintGuide_percent="0.11" />

                <android.support.constraint.Guideline
                    android:id="@+id/right_guideline"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintGuide_percent="0.89" />

            </android.support.constraint.ConstraintLayout>

这是蓝图,其中两个 TextView (包含在@+id/contentLayout 中)都在 ImageView 下方 enter image description here

最佳答案

问题在于 TextView 的宽度。您已将其设置为 wrap_content。这就造成了问题。只需在布局编辑器中将其更改为 match_constraints 或在 xml 代码中手动将宽度设置为 0dp 即可正常工作。

关于android - 约束布局指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50119992/

相关文章:

Java Libgdx 获取截图非常慢

java - LibGDX 如何将游戏放入表格中? *** 固定的 ***

android - 无法将 FrameLayout 放置在 ConstraintLayout 内

android:windowTranslucentStatus/fitsSystemWindows 工具栏大小错误

使用 layout_weight、layout_width 和 maxWidth 的 Android 布局

android - 将 layout_constraintWidth_default 与 spread 或 wrap 一起使用没有区别吗?

Android Material CardView 与 ConstraintLayout 剪辑子项不起作用

android - 当用户将手机放在他/她的耳朵附近时,在我的 Activity 中禁用 UI?

java - 为什么 ListView 需要将其容器项放在单独的布局文件中

Android约束布局水平划分