首页 > 解决方案 > ConstraintLayout 宽度 wrap_content 或 0dp

问题描述

我有这样的布局。

<?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="wrap_content"
    android:background="#ededed"
    android:padding="1dp"
    android:layout_height="wrap_content"
    tools:context="com.guna.testapplication.MainActivity">

    <TextView
        android:id="@+id/tvHeader"
        style="@style/TextAppearance.AppCompat.Headline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#fdfdfd"
        android:text="Hello world"
        app:layout_constraintBottom_toTopOf="@+id/tvContent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello world"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvHeader" />
</android.support.constraint.ConstraintLayout>

这给了我以下输出

在此处输入图像描述

这没问题。但如果tvHeader只有文本Hello

在此处输入图像描述

看,标题width更改为较小的标题并对齐中心。

好的,如果我更改了layout_widthoftvHeader0dp查看输出。

在此处输入图像描述

在此处输入图像描述

所以,我的问题是,我希望我的布局应该尽可能地水平扩展,我该如何实现呢?没必要用ConstraintLayoutLinearLayout对我来说也够用了。

标签: androidandroid-layout

解决方案


在您的 textView 中使用android:maxlines="1"属性,以便将文本限制为仅一行,这样做不会让 textview 垂直扩展。还将文本视图包装在垂直线性布局中

我还优化了您的布局,因为它是理想的距屏幕 16dp 的边距,并且在同级视图之间留有一点边距。并且还重新安排了约束。试试这个代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:background="#ededed"
    android:padding="1dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/tvHeader"
            style="@style/TextAppearance.AppCompat.Headline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#fdfdfd"
            android:maxLines="1"
            android:text="Hello world"
             />

        <TextView
            android:id="@+id/tvContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Hello world" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

推荐阅读