属性 wrap_content 无效

The property wrap_content is not working

我必须使 TextView 在 LinearLayout 内水平,在 ConstraintLayout 内垂直。
问题是 TextView 的 属性 wrap_content 不工作。
这是 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.black_swan.myapplication11.MainActivity"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <LinearLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:orientation="vertical"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />
        </LinearLayout>

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

这是项目的图片:

删除 TextView 上的 android:layout_weight="1"。更进一步看来你的TextView'sLinearLayout没有用。尽量不要使用不必要的嵌套布局。

如果您使用布局权重,通常,您会为相应的方向使用 0dp 的大小。

  • 水平宽度为 0dp
  • 0dp 垂直高度

当您使用权重时,wrap_content 大小将被覆盖