代码之家  ›  专栏  ›  技术社区  ›  Harikrishnan

约束布局获取imageview的默认上边距

  •  2
  • Harikrishnan  · 技术社区  · 7 年前

    我正在尝试使用constraint layout创建一个布局,上面有一个ImageView,ImageView中有一个按钮,下面有一个TextView,然后在下面有一个TextView。以下是xml:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
        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="wrap_content"
        android:layout_margin="10dp"
        app:cardElevation="4dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white">
            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:id="@+id/news_image1"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    app:layout_constraintDimensionRatio="16:9"
                    android:scaleType="centerCrop"
                    android:adjustViewBounds="true"
                    app:layout_constraintTop_toTopOf="parent"
                    android:layout_marginTop="0dp"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintBottom_toTopOf="@+id/news_title1"/>
                <ImageButton
                    android:id="@+id/news_share_button_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:src="@drawable/share_button_big"
                    app:layout_constraintBottom_toBottomOf="@+id/news_image1"
                    app:layout_constraintRight_toRightOf="@+id/news_image1"
                    android:layout_marginRight="15dp"
                    android:layout_marginEnd="15dp"/>
                <TextView
                    android:id="@+id/news_title1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:gravity="center_vertical|start"
                    android:layout_alignParentBottom="true"
                    android:lines="3"
                    android:ellipsize="end"
                    app:layout_constraintTop_toBottomOf="@+id/news_image1"
                    app:layout_constraintBottom_toTopOf="@+id/news_read_more1"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    android:textColor="@color/colorPrimaryText"
                    android:layout_margin="@dimen/news_cell0_textview_margin"
                    android:textSize="12sp"
                    android:typeface="monospace" />
                <TextView
                    android:id="@+id/news_read_more1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:gravity="center_vertical|start"
                    android:layout_alignParentBottom="true"
                    android:lines="1"
                    android:ellipsize="end"
                    app:layout_constraintTop_toBottomOf="@+id/news_title1"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    android:textColor="@color/read_more_text_color"
                    android:text="@string/news_read_more"
                    android:layout_margin="@dimen/news_cell0_textview_margin"
                    android:textSize="10sp" />
            </android.support.constraint.ConstraintLayout>
        </RelativeLayout>
    </android.support.v7.widget.CardView>
    

    enter image description here

    请注意ImageView顶部和卡之间的边距,这正是困扰我的地方。

    3 回复  |  直到 7 年前
        1
  •  3
  •   Ben P.    7 年前

    你的 news_image , news_title1 news_read_more1 视图形成一个链。显然,虽然我找不到有关这方面的文档,但垂直链中的所有视图都将共享垂直边距。换句话说,应用 layout_marginTop layout_marginBottom 属性将最终应用于 .

    你必须重新分配你的利润,记住这一点。

    看起来这个行为比我想象的要复杂一点。首先,它看起来只适用于具有 spread “链样式”(默认设置)。其次,看起来上边距应用于指定的视图以及所有视图 在上面 在下面

    至于在你的具体案例中如何解决这个问题?您应该能够使用左/右页边距而不会出现问题。然后,你可能应该在顶视图上使用下边距来隔开这三个视图。

    编辑#2

    app:layout_constraintBottom_toTopOf="@+id/news_title1" 从ImageView的约束,这将打破链条,现在垂直边距不共享。

        2
  •  2
  •   Harikrishnan    7 年前

    this great tutorial

    1. 传播 app:layout_constraintHorizontal_chainStyle=”spread” app:layout_constraintVertical_chainStyle=”spread”
    2. 内部蔓延 :第一个和最后一个视图附着在链每一端的约束上,其余视图均匀分布。例如。 app:layout_constraintHorizontal_chainStyle=”spread_inside” app:layout_constraintVertical_chainStyle=”spread_inside”
    3. 拥挤的 app:layout_constraintHorizontal_chainStyle=”packed” app:layout_constraintVertical_chainStyle=”packed”
    4. 加权 thelayout_constraintHorizontal_weight layout_constraintVertical_weight attributes .如果你熟悉 layout_weight 在线性布局中,其工作方式相同。因此,权重值最高的视图获得最多的空间;具有相同权重的视图获得相同的空间量。

    spread spread_inside 因此,第一个和最后一个视图附加到链每一端的约束上(因此遵守提供的边距)。xml现在看起来像:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
        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="wrap_content"
        android:layout_margin="10dp"
        app:cardElevation="4dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white">
            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:id="@+id/news_image1"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    app:layout_constraintDimensionRatio="16:9"
                    android:scaleType="centerCrop"
                    android:adjustViewBounds="true"
                    app:layout_constraintTop_toTopOf="parent"
                    android:layout_marginTop="0dp"
                    app:layout_constraintVertical_chainStyle="spread_inside"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintBottom_toTopOf="@+id/news_title1"/>
                <ImageButton
                    android:id="@+id/news_share_button_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:src="@drawable/share_button_big"
                    app:layout_constraintBottom_toBottomOf="@+id/news_image1"
                    app:layout_constraintRight_toRightOf="@+id/news_image1"
                    android:layout_marginRight="15dp"
                    android:layout_marginEnd="15dp"/>
                <TextView
                    android:id="@+id/news_title1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:gravity="center_vertical|start"
                    android:layout_alignParentBottom="true"
                    android:lines="3"
                    android:ellipsize="end"
                    app:layout_constraintVertical_chainStyle="spread_inside"
                    app:layout_constraintTop_toBottomOf="@+id/news_image1"
                    app:layout_constraintBottom_toTopOf="@+id/news_read_more1"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    android:textColor="@color/colorPrimaryText"
                    android:layout_margin="@dimen/news_cell0_textview_margin"
                    android:textSize="12sp"
                    android:typeface="monospace" />
                <TextView
                    android:id="@+id/news_read_more1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:gravity="center_vertical|start"
                    android:layout_alignParentBottom="true"
                    android:lines="1"
                    android:ellipsize="end"
                    app:layout_constraintVertical_chainStyle="spread_inside"
                    app:layout_constraintTop_toBottomOf="@+id/news_title1"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    android:textColor="@color/read_more_text_color"
                    android:text="@string/news_read_more"
                    android:layout_margin="@dimen/news_cell0_textview_margin"
                    android:textSize="10sp" />
            </android.support.constraint.ConstraintLayout>
        </RelativeLayout>
    </android.support.v7.widget.CardView>
    
        3
  •  1
  •   Muthukrishnan Rajendran    7 年前

    试试这个,我删除 app:layout_constraintTop_toTopOf="parent"

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
        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="wrap_content"
        android:layout_margin="10dp"
        app:cardElevation="4dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white">
            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <ImageView
                    android:id="@+id/news_image1"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    app:layout_constraintDimensionRatio="16:9"
                    android:scaleType="centerCrop"
                    android:adjustViewBounds="true"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"/>
                <ImageButton
                    android:id="@+id/news_share_button_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:src="@drawable/ic_menu_share"
                    app:layout_constraintBottom_toBottomOf="@+id/news_image1"
                    app:layout_constraintRight_toRightOf="@+id/news_image1"
                    android:layout_marginRight="15dp"
                    android:layout_marginEnd="15dp"/>
                <AliasTextView
                    android:id="@+id/news_title1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:gravity="center_vertical|start"
                    android:layout_alignParentBottom="true"
                    android:lines="3"
                    android:ellipsize="end"
                    app:layout_constraintTop_toBottomOf="@+id/news_image1"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    android:textColor="@color/colorPrimaryText"
                    android:layout_margin="@dimen/news_cell0_textview_margin"
                    android:textSize="12sp"
                    android:typeface="monospace" />
                <TextView
                    android:id="@+id/news_read_more1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:gravity="center_vertical|start"
                    android:layout_alignParentBottom="true"
                    android:lines="1"
                    android:ellipsize="end"
                    app:layout_constraintTop_toBottomOf="@+id/news_title1"
                    app:layout_constraintLeft_toLeftOf="parent"
                    android:textColor="@color/read_more_text_color"
                    android:text="@string/news_read_more"
                    android:layout_margin="@dimen/news_cell0_textview_margin"
                    android:textSize="10sp" />
            </android.support.constraint.ConstraintLayout>
        </RelativeLayout>
    </android.support.v7.widget.CardView>