【问题标题】:The property wrap_content is not working属性 wrap_content 不起作用
【发布时间】:2017-08-23 16:51:04
【问题描述】:

我必须在 LinearLayout 中使 TextView 水平,在 ConstraintLayout 中使 TextView 垂直。
问题是 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>

这是项目的图片:

【问题讨论】:

  • 停止使用嵌套的 LinearLayout 或移除 layout_weight
  • 没有嵌套的 linerlayout 包装内容工作正常,但为什么它不适用于嵌套的 linerlayout @cricket_007
  • 因为 layout_weight 覆盖了 wrap_content
  • 好的,谢谢@cricket_007

标签: android android-linearlayout android-wrap-content


【解决方案1】:

删除TextView 上的android:layout_weight="1"。此外,您的TextView'sLinearLayout 似乎没有用。尽量不要使用不必要的嵌套布局。

【讨论】:

  • 当我在 TextView 上删除 android:layout_weight="1" 时它会起作用,谢谢......我会在 2 分钟后接受你的回答
【解决方案2】:

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

  • 0dp 横向宽度
  • 0dp 垂直高度

当你使用权重时,wrap_content 的大小会被覆盖

【讨论】:

    猜你喜欢
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    相关资源
    最近更新 更多