我如何在约束布局中朝正确的方向移动视图

时间:2019-05-13 17:33:46

标签: android space android-constraintlayout

  

我正在开发新闻应用程序,我想在右侧移动视图   约束布局中的方向。但是我可以在下面实现我想要的   我已在其中实施约束布局的XML文件的任何建议   和技巧大受赞赏。

我正在开发新闻应用,我想在右侧移动视图

  约束布局中的

方向。但是我可以在下面实现我想要的   我已在其中实施约束布局的XML文件的任何建议   和技巧大受赞赏。

<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="match_parent">

    <ImageView

        android:id="@+id/articleImage"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_margin="8dp"
        app:layout_constraintDimensionRatio="16:9"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline4"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/articleAuthor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:gravity="start"
        android:text="article_author"
        android:textSize="12sp"
        app:layout_constraintEnd_toStartOf="@+id/articleImage"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/articleTitle"
        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"

        android:layout_marginEnd="8dp"

        android:layout_marginBottom="8dp"

        android:text="article_title"

        android:textSize="12sp"


        app:layout_constraintEnd_toStartOf="@+id/guideline4"

        app:layout_constraintHorizontal_bias="0"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/articleAuthor" />


    <TextView

        android:id="@+id/articleTime"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginStart="8dp"

        android:layout_marginLeft="8dp"

        android:layout_marginTop="8dp"

        android:layout_marginBottom="8dp"

        android:text="Article Time"

        android:textSize="18sp"

        app:layout_constraintEnd_toStartOf="@+id/articleFavorite"

        app:layout_constraintHorizontal_bias="0"

        app:layout_constraintHorizontal_chainStyle="packed"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/articleTitle" />


    <ImageButton

        android:id="@+id/articleFavorite"
        android:layout_width="32dp"
        android:layout_height="32dp"

        android:background="@color/colorWhite"

        android:src="@drawable/ic_bookmark"

        app:layout_constraintBottom_toBottomOf="@+id/articleTime"

        app:layout_constraintEnd_toStartOf="@+id/articleShare"
        app:layout_constraintStart_toEndOf="@+id/articleTime"
        app:layout_constraintTop_toTopOf="@+id/articleTime" />


    <ImageButton

        android:id="@+id/articleShare"
        android:layout_width="32dp"
        android:layout_height="32dp"

        android:layout_marginEnd="8dp"

        android:layout_marginRight="8dp"

        android:background="@color/colorWhite"

        android:contentDescription="TODO"

        android:src="@drawable/ic_share"

        app:layout_constraintBottom_toBottomOf="@+id/articleTime"

        app:layout_constraintEnd_toStartOf="@+id/guideline4"
        app:layout_constraintStart_toEndOf="@+id/articleFavorite"
        app:layout_constraintTop_toTopOf="@+id/articleTime" />


    <android.support.constraint.Guideline

        android:id="@+id/guideline4"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="vertical"

        app:layout_constraintGuide_percent="0.6" />


</android.support.constraint.ConstraintLayout>

该应用的当前屏幕截图下方 app screenshot 下面的屏幕快照,我想要实现screenshot  I want

3 个答案:

答案 0 :(得分:2)

用另一个视图组包装视图(再次说ConstraintLayout),并将app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"设置为此视图组的约束。然后,该视图将水平居中。

编辑:代码

<android.support.constraint.ConstraintLayout android:layout_height="wrap_content" android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.constraint.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent">

    <ImageView

        android:id="@+id/articleImage"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_margin="8dp"
        app:layout_constraintDimensionRatio="16:9"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline4"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/articleAuthor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:gravity="start"
        android:text="article_author"
        android:textSize="12sp"
        app:layout_constraintEnd_toStartOf="@+id/articleImage"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/articleTitle"
        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"

        android:layout_marginEnd="8dp"

        android:layout_marginBottom="8dp"

        android:text="article_title"

        android:textSize="12sp"


        app:layout_constraintEnd_toStartOf="@+id/guideline4"

        app:layout_constraintHorizontal_bias="0"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/articleAuthor" />


    <TextView

        android:id="@+id/articleTime"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginStart="8dp"

        android:layout_marginLeft="8dp"

        android:layout_marginTop="8dp"

        android:layout_marginBottom="8dp"

        android:text="Article Time"

        android:textSize="18sp"

        app:layout_constraintEnd_toStartOf="@+id/articleFavorite"

        app:layout_constraintHorizontal_bias="0"

        app:layout_constraintHorizontal_chainStyle="packed"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/articleTitle" />


    <ImageButton

        android:id="@+id/articleFavorite"
        android:layout_width="32dp"
        android:layout_height="32dp"

        android:background="@color/"

        android:src="@drawable/"

        app:layout_constraintBottom_toBottomOf="@+id/articleTime"

        app:layout_constraintEnd_toStartOf="@+id/articleShare"
        app:layout_constraintStart_toEndOf="@+id/articleTime"
        app:layout_constraintTop_toTopOf="@+id/articleTime" />


    <ImageButton

        android:id="@+id/articleShare"
        android:layout_width="32dp"
        android:layout_height="32dp"

        android:layout_marginEnd="8dp"

        android:layout_marginRight="8dp"

        android:background="@color/"

        android:contentDescription="TODO"

        android:src="@drawable/"

        app:layout_constraintBottom_toBottomOf="@+id/articleTime"

        app:layout_constraintEnd_toStartOf="@+id/guideline4"
        app:layout_constraintStart_toEndOf="@+id/articleFavorite"
        app:layout_constraintTop_toTopOf="@+id/articleTime" />


    <android.support.constraint.Guideline

        android:id="@+id/guideline4"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.6" />
</android.support.constraint.ConstraintLayout>

我稍微弄乱了ImageButtons,请修复可绘制对象和颜色。 编辑2:由于某种原因,这里的代码部分中不会显示另一个结束标记。确保在末尾添加另一个ConstraintLayout结束标记。

<android.support.constraint.ConstraintLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.Guideline
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.55"
    android:id="@+id/guideline"/>
<ImageView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@id/guideline"
    app:layout_constraintDimensionRatio="16:9"
    android:layout_margin="16dp"
    android:id="@+id/imageView"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    android:id="@+id/mainTextView"
    android:layout_marginStart="25dp"
    android:layout_marginTop="10dp"
    android:text="Placeholder"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@id/mainTextView"
    app:layout_constraintStart_toStartOf="@id/mainTextView"
    android:layout_marginTop="5dp"
    android:text="Secondary"
    android:id="@+id/secondaryTextView"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@id/secondaryTextView"
    app:layout_constraintStart_toStartOf="@id/mainTextView"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="5dp"
    android:text="Tertiary"
    android:id="@+id/tertiaryTextView"/>

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="@id/tertiaryTextView"
    app:layout_constraintBottom_toBottomOf="@id/tertiaryTextView"
    app:layout_constraintStart_toEndOf="@id/tertiaryTextView"
    android:layout_marginStart="15dp"
    android:id="@+id/firstButton"/>
<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="@id/firstButton"
    app:layout_constraintBottom_toBottomOf="@id/firstButton"
    app:layout_constraintStart_toEndOf="@id/firstButton"
    android:layout_marginStart="15dp"
    android:id="@+id/secondButton"/>

编辑3:这是一个干净的选择,这个应该可以做你想要的。虽然您必须按照所需的方式调整边距!再次关闭标签...我不知道为什么它不会出现在这里!

答案 1 :(得分:1)

如果要将屏幕左侧的视图移动到屏幕右侧,只需更改约束,如下所示:

替换约束,您的视图将在屏幕的左侧和右侧作为您的约束获得约束。

这样做:

<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="match_parent">

<ImageView

    android:id="@+id/articleImage"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_gravity="center"
    android:layout_margin="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintDimensionRatio="16:9"
    app:layout_constraintEnd_toStartOf="@+id/guideline4"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<TextView
    android:id="@+id/articleAuthor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:gravity="start"
    android:text="article_author"
    android:textSize="12sp"
    app:layout_constraintEnd_toStartOf="@+id/articleImage"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="@+id/guideline4"
    app:layout_constraintTop_toTopOf="parent" />


<TextView
    android:id="@+id/articleTitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="article_title"
    android:textSize="12sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="@+id/guideline4"
    app:layout_constraintTop_toBottomOf="@+id/articleAuthor" />


<TextView
    android:id="@+id/articleTime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Article Time"
    android:textSize="18sp"
    app:layout_constraintEnd_toStartOf="@+id/articleFavorite"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintStart_toStartOf="@+id/guideline4"
    app:layout_constraintTop_toBottomOf="@+id/articleTitle" />


<ImageButton
    android:id="@+id/articleFavorite"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@color/colorWhite"
    android:src="@drawable/ic_bookmark"
    app:layout_constraintBottom_toBottomOf="@+id/articleTime"
    app:layout_constraintEnd_toStartOf="@+id/articleShare"
    app:layout_constraintStart_toEndOf="@+id/articleTime"
    app:layout_constraintTop_toTopOf="@+id/articleTime" />


<ImageButton
    android:id="@+id/articleShare"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:background="@color/colorWhite"
    android:contentDescription="TODO"
    android:src="@drawable/ic_share"
    app:layout_constraintBottom_toBottomOf="@+id/articleTime"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/articleFavorite"
    app:layout_constraintTop_toTopOf="@+id/articleTime"
    app:layout_constraintVertical_bias="0.0" />


<android.support.constraint.Guideline
    android:id="@+id/guideline4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.6" />

</android.support.constraint.ConstraintLayout>

它看起来像这样:

enter image description here

答案 2 :(得分:0)

将ImageView(id:articleImage)的 layout_gravity 属性值从中心更改为右边。

希望我的回答对您有所帮助。