marginTop不适用于ConstraintLayout和wrap_content

Sim*_*fer 15 android android-layout android-xml android-constraintlayout android-wrap-content

在我的Fragment中我有一个ConstraintLayout,layout_height="wrap_content"我希望在视图底部的两个底部之间有一个边距.

当我将这个边距添加layout_marginBottom到上方按钮(button_welcome_signup)时,似乎工作正常.但是,如果我尝试将其添加到底部按钮(button_welcome_signin),因为layout_marginTop它不起作用.

有谁知道这里的问题/如果我做错了什么?

(请注意,我使用wrap_content是有原因的,而且我非常想在底部按钮上使用边距,因此我可以将其添加到其样式中,以便在我的项目中实现更好的UI一致性).

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:MyAppApp="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:background="@color/white"
    android:minHeight="@dimen/min_height_welcome_frame"
    android:padding="@dimen/margin_all_frame_inner">

    <ImageView
        android:id="@+id/imageview_welcome_logo"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/logo_header"
        MyAppApp:layout_constraintTop_toTopOf="parent"
        MyAppApp:layout_constraintLeft_toLeftOf="parent"
        MyAppApp:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/textiew_welcome_title"
        style="@style/MyAppTextViewTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin_all_component_l"
        android:text="@string/welcome_title"
        MyAppApp:layout_constraintTop_toBottomOf="@id/imageview_welcome_logo" />

    <TextView
        android:id="@+id/textview_welcome_text"
        style="@style/MyAppTextViewText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/welcome_message"
        MyAppApp:layout_constraintTop_toBottomOf="@id/textiew_welcome_title" />

    <Button
        android:id="@+id/button_welcome_signin"
        style="@style/MyAppSubButton"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:layout_marginTop="@dimen/margin_all_component_s" 
        android:text="@string/welcome_sign_in"
        MyAppApp:layout_constraintBottom_toBottomOf="parent" />

    <Button
        android:id="@+id/button_welcome_signup"
        style="@style/MyAppButton"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:layout_marginTop="@dimen/margin_all_component_l"
        android:text="@string/welcome_sign_up"
        MyAppApp:layout_constraintBottom_toTopOf="@id/button_welcome_signin"
        MyAppApp:layout_constraintTop_toBottomOf="@id/textview_welcome_text"
        MyAppApp:layout_constraintVertical_bias="1" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

styles.xml:

<style name="MyAppButton" parent="Widget.AppCompat.Button">
    <item name="android:background">@drawable/button_selector_blue</item>
    <item name="android:textSize">@dimen/textsize_all_l</item>
    <item name="android:textColor">@color/white</item>
    <item name="fontFamily">@font/my_sans_serif_regular</item>
</style>

<style name="MyAppSubButton" parent="Widget.AppCompat.Button">
    <item name="android:background">@drawable/button_selector_transparent</item>
    <item name="android:textSize">@dimen/textsize_all_l</item>
    <item name="android:textColor">@color/turquoise_blue</item>
    <item name="fontFamily">@font/my_sans_serif_regular</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助.

Ben*_* P. 26

在a中ConstraintLayout,子视图的边边距仅在该边被约束到另一视图时才会生效.在原始示例中,顶部按钮上的下边距有效,因为顶部按钮具有底部约束:

MyAppApp:layout_constraintBottom_toTopOf="@id/button_welcome_signin"
Run Code Online (Sandbox Code Playgroud)

但是,底部按钮的上边距不起作用,因为底部按钮的顶部没有约束.

如果您想在底部按钮上使用上边距,请添加此约束:

MyAppApp:layout_constraintTop_toBottomOf="@+id/button_welcome_signup"
Run Code Online (Sandbox Code Playgroud)

请注意,您必须通过将此属性添加到顶部按钮来更新链样式(因为此新约束会创建链):

MyAppApp:layout_constraintVertical_chainStyle="packed"
Run Code Online (Sandbox Code Playgroud)

  • 不知道关于利润的一个:)如果不受限制,它们会生效,请+1。 (2认同)