简体   繁体   English

marginTop 不适用于 ConstraintLayout 和 wrap_content

[英]marginTop does not work with ConstraintLayout and wrap_content

In my Fragment I have a ConstraintLayout with layout_height="wrap_content" and I would like to have a margin between my two buttons at the bottom of the view.在我的片段中,我有一个带有layout_height="wrap_content"的 ConstraintLayout,我希望在视图底部的两个按钮之间有一个边距。

When I add this margin as layout_marginBottom to the upper button ( button_welcome_signup ) it seems to work fine.当我将此边距作为layout_marginBottom添加到上部按钮( button_welcome_signup )时,它似乎工作正常。 However, if I try to add it to the bottom button ( button_welcome_signin ) as layout_marginTop it doesn't work.但是,如果我尝试将它添加到底部按钮 ( button_welcome_signin ) 作为layout_marginTop它不起作用。

Does anybody know what the issues here is / if am doing something wrong?有谁知道这里的问题是什么/如果我做错了什么?

(Please note there is a reason why I am using wrap_content and also that I would seriously like to use the margin on the bottom button, so I can add some margin to its style for better UI consistency within my project). (请注意,我使用 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>

styles.xml:样式.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>

Within a ConstraintLayout , side margins for a child view will only take effect if that side is constrained to another view.ConstraintLayout ,子视图的侧边距仅在该侧被约束到另一个视图时才会生效。 In your original example, bottom margin on the top button works because the top button has a bottom constraint:在您的原始示例中,顶部按钮的底部边距有效,因为顶部按钮具有底部约束:

MyAppApp:layout_constraintBottom_toTopOf="@id/button_welcome_signin"

However, top margin on the bottom button doesn't work because the bottom button has no constraint for its top.但是,底部按钮的顶部边距不起作用,因为底部按钮对其顶部没有约束。

If you would like to use top margin on the bottom button, add this constraint:如果您想在底部按钮上使用上边距,请添加以下约束:

MyAppApp:layout_constraintTop_toBottomOf="@+id/button_welcome_signup"

Note that you will also have to update the chain style (since this new constraint creates a chain) by adding this attribute to the top button:请注意,您必须通过将此属性添加到顶部按钮来更新链样式(因为此新约束创建了一个链):

MyAppApp:layout_constraintVertical_chainStyle="packed"

try this试试这个

<Button
    android:id="@+id/button_welcome_signin"
    style="@style/MyAppSubButton"
    android:layout_width="match_parent"
    android:layout_height="46dp"
    android:layout_marginTop="16dp"
    android:text="@string/welcome_sign_in"
    MyAppApp:layout_constraintBottom_toBottomOf="parent"
    MyAppApp:layout_constraintEnd_toEndOf="parent"
    MyAppApp:layout_constraintStart_toStartOf="parent"
    MyAppApp:layout_constraintTop_toBottomOf="@+id/button_welcome_signup" />

First image am removed margins on Your code ,第一张图片已删除您的代码上的边距,

在此处输入图片说明

Second image added margin on left,right,top,bottom第二张图片在左侧、右侧、顶部、底部添加了边距

在此处输入图片说明

<Button
        android:id="@+id/SaveBtnId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Save"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="16dp"
        app:layout_constraintRight_toRightOf="parent"
         />

在此处输入图片说明

It took me a while to figure out I was thinking about this the wrong way, so maybe it will help someone else.我花了一段时间才发现我在以错误的方式思考这个问题,所以也许它会帮助其他人。

In a ConstraintLayout, it is making a margin from the constraint .在 ConstraintLayout 中,它从约束中留出边距。

In other words, let's say you have two buttons next to each other.换句话说,假设您有两个相邻的按钮。

The one on the right is constrained StartToEnd , TopToTop , and BottomToBottom to the one on the left.右侧的将StartToEndTopToTopBottomToBottom约束为左侧的。

If you give the right one a marginBottom , it's not going to push things below it down, it's going to push itself up from the bottom of the first button.如果你给右边的一个marginBottom ,它不会把它下面的东西推下来,它会从第一个按钮的底部向上推。 The margin is between its bottom line and the bottom line of the thing it's constrained to, not between all views on the screen.边距位于它的底线和它所约束事物的底线之间,而不是在屏幕上的所有视图之间。

In case someone has the same problem I was having, trying to have a View(RecyclerView) as match_parent at the bottom of another View (which is an ImageView).如果有人遇到我遇到的相同问题,请尝试将 View(RecyclerView) 作为 match_parent 在另一个 View(这是一个 ImageView)的底部。 But the RecycleView was overlapping my ImageView even tho I was putting a top_margin in my RecyclerView of my ImageView height size.但是即使我在我的 ImageView 高度大小的 RecyclerView 中放置了一个 top_margin , RecycleView 也重叠了我的 ImageView 。

Solution: Set the - android:layout_height="0dp" - works great for me.解决方案:设置 - android:layout_height="0dp" - 非常适合我。 The explanation is bellow.解释如下。

MATCH_PARENT not working MATCH_PARENT 不起作用

<View
    android:id="@+id/view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"/>

Any view should apply the attribute rules of its parent ViewGroup.任何视图都应该应用其父 ViewGroup 的属性规则。 ConstraintLayout supports the “0dp” value (match_constraint) instead of “match_parent” to get the match parent behavior. ConstraintLayout 支持“0dp”值(match_constraint)而不是“match_parent”来获取匹配的父行为。 So, NEVER EVER use “match_parent” with ConstraintLayout!所以,永远不要在 ConstraintLayout 中使用“match_parent”!

I got from here... https://medium.com/@jemli.idea/constraintlayout-never-ever-97c121286100我从这里... https://medium.com/@jemli.idea/constraintlayout-never-ever-97c121286100

I was using android:margin_vertical and android:margin_top at the same time, here margin_vertical takes precedence.我同时使用android:margin_verticalandroid:margin_top ,这里margin_vertical优先。

issue can occur with android:margin as well android:margin也可能出现问题

android:layout_marginTop="@dimen/_10sdp"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM