框架布局设置高度与宽度相同

Frame layout set height same as width

在我的布局中,我使用 FrameLayout,宽度设置为 wrap_content,这取决于其中 TextView 的宽度。现在我希望该布局的高度与宽度相同。我怎样才能做到这一点?

使用 ConstraintLayout 而不是 FrameLayout 你的 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="MyText"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>