当前位置: 首页 > 知识库问答 >
问题:

为什么在这个ConstraintLayout中0dp与wrap_content相同?

黄景胜
2023-03-14

刚接触XML并尝试使用XML实验室(https://github.com/harshitha-akkaraju/layoutlab)

让我困惑的是,即使按钮的宽度是0dp,但如果宽度是wrap_content的话,它也是。我知道0dp相当于ConstraintLayout中的“match_constraint”。然而,那不是意味着0dp应该填满尽可能多的空间吗?

<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">

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp"
        android:text="1"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
</android.support.constraint.ConstraintLayout>

共有1个答案

邓禄
2023-03-14

为了使按钮与父布局相匹配,您需要设置两个相反的约束....如

<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">

    <Button
        android:id="@+id/button1"
<!-- Since you are setting width as 0dp, i presume you want the width to match... -->
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp"
        android:text="1"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
<!-- You must add here another constraint to the right -->
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
</android.support.constraint.ConstraintLayout>```
 类似资料:
  • 问题内容: 从2010年的计算机语言基准游戏中可以看出: Go平均比C慢10倍 Go比Java慢3倍! 考虑到Go编译器会生成要执行的本机代码,这怎么可能? Go的编译器不成熟?还是Go语言存在一些内在问题? 编辑: 大多数答案否认Go语言的内在缓慢,声称问题出在不成熟的编译器中。 因此,我进行了一些自己的测试来计算斐波那契数:迭代算法在Go(freebsd,6g)中以与C(带有O3选项)一样的速

  • 为什么它不打印“processTextPosition:ContainsKey”?

  • 我正在将RecolyerView的xml项从LinearLayout更改为ConstraintLayout。当我水平滚动recyclerView时,它会滞后,并且呈现速度比LinearLayout慢得多。 约束布局 线性布局

  • 问题内容: 这里是Spark新手。我最近开始使用以下命令在两个内核的本地计算机上使用Spark: 我有一个393Mb的文本文件,其中包含近一百万行。我想执行一些数据操作操作。我使用的是内置PySpark的数据帧的功能进行简单的操作,如,,,。 但是,当我在完全相同的数据集上对熊猫进行完全相同的操作时,就延迟而言,熊猫似乎在很大程度上击败了pyspark。 我想知道这可能是什么原因。我有几点想法。

  • 问题内容: 我想迭代地构建稀疏矩阵,并注意到根据SciPy文档,有两种合适的选择: LiL矩阵: 类scipy.sparse.lil_matrix(arg1,shape = None,dtype = None,copy = False)[源]基于行的链表稀疏矩阵 这是用于增量构造稀疏矩阵的有效结构。 DoK矩阵: 类scipy.sparse.dok_matrix(arg1,shape = None

  • 我需要帮助我的错误就像 代码: