没有`weight`属性时android:layout_width =“ 0dp”是什么意思?

时间:2018-11-07 08:36:31

标签: android android-layout-weight

我有这样的布局:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlayGoogleMaterialIcons">

  <LinearLayout
      android:layout_height="wrap_content"
      android:layout_width="0dp">

...

android:layout_width =“ 0dp”的含义是什么? LinearLayout?

当没有weight属性时android:layout_width =“ 0dp”是什么意思?

1 个答案:

答案 0 :(得分:1)

对于ConstraintLayout的子代,如果您已设置约束,则0dp用于match_constraint(采用全宽或全高)

  

使用0dp,与“ MATCH_CONSTRAINT”等效   https://developer.android.com/reference/android/support/constraint/ConstraintLayout

示例

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

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:text="text1" />

    <TextView
        android:id="@+id/tv_2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toStartOf="@id/tv_3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_1"
        tools:text="text2" />

    <TextView
        android:id="@+id/tv_3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/tv_2"
        app:layout_constraintTop_toBottomOf="@id/tv_1"
        tools:text="text3" />
</android.support.constraint.ConstraintLayout>

enter image description here

在上面的代码(和图像)中,您看到text1 TextView的宽度是写文本text1所需的宽度

对于text2和text3 TextView,它需要将整个宽度除以2即约束条件所说的