【问题标题】:What does app:layout_goneMarginLeft="" do for views in constraintlayout?app:layout_goneMarginLeft="" 对约束布局中的视图有什么作用?
【发布时间】:2019-01-08 08:12:19
【问题描述】:

app:layout_goneMarginLeft 及其变体如何影响约束布局中的视图排列?

【问题讨论】:

    标签: android android-layout android-studio android-view android-constraintlayout


    【解决方案1】:

    希望这个例子能帮助你理解这个简单的概念 -

    考虑2 TextViews 的ID 为textView1textView2,其中textView2textView1 的末尾有0dp 约束。

    案例 1:当textView1 的可见性为VISIBLE 时,textView2 将恰好位于textView1 的右侧,边距为 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="100dp">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/form_field_background"
            android:padding="5dp"
            android:text="TextView1"
            android:visibility="visible" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/form_field_background"
            android:padding="5dp"
            android:text="TextView2"
            app:layout_constraintStart_toEndOf="@+id/textView1"
            app:layout_goneMarginLeft="10dp"
            app:layout_goneMarginStart="10dp" />
    
    </android.support.constraint.ConstraintLayout>
    

    结果 -

    案例 2:当textView1 的可见性为GONE 时,textView2 会将其 marginLeft 设置为 10dp,因为我指定了app:layout_goneMarginLeft="10dp"

    <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="100dp">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/form_field_background"
            android:padding="5dp"
            android:text="TextView1"
            android:visibility="gone" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/form_field_background"
            android:padding="5dp"
            android:text="TextView2"
            app:layout_constraintStart_toEndOf="@+id/textView1"
            app:layout_goneMarginLeft="10dp"
            app:layout_goneMarginStart="10dp" />
    
    </android.support.constraint.ConstraintLayout>
    

    结果 -

    【讨论】:

      【解决方案2】:

      例如,当您将视图 B 约束到另一个视图 A 并且在您的逻辑/代码中的某个时刻,您将视图 A 的可见性更改为 view.GONE,例如,如果您使用 app:layout_goneMarginLeft="",它将保留边距或者像文档中所说的那样:

      您还可以指定要使用的不同边距值

      你可以找到一个例子here

      【讨论】:

        猜你喜欢
        • 2021-05-28
        • 2017-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-23
        • 2017-08-11
        • 1970-01-01
        • 2018-08-30
        相关资源
        最近更新 更多