android - 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_width
oftvHeader
以0dp
查看输出。
所以,我的问题是,我希望我的布局应该尽可能地水平扩展,我该如何实现呢?没必要用ConstraintLayout
,LinearLayout
对我来说也够用了。
解决方案
在您的 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>
推荐阅读
- java - 单击android上的手机硬件后退按钮时从未调用过onResume?
- python - 使用 PyQt5 提取网页的某些部分并显示它们
- javascript - Firebase 数据库数据检索遇到问题
- reactjs - React:componentDidUpdate - 当我更新数组中的对象时,以前的状态与当前状态相同
- drupal - Drupal JSON:API 核心模块。无法使用授权
- javascript - Javascript根据另一个元素提取数组中的元素
- r - R: data.table 同时对多个数据集进行子集化
- ios - 以编程方式放大 UIScrollview 不起作用
- python-3.x - 无法让我的函数返回正确的布尔值
- r - 摘要SE图看起来不正确