【问题标题】:Move View out of Constraint Layout将视图移出约束布局
【发布时间】:2018-08-30 07:33:54
【问题描述】:

我想移动我的ImageView,这样它就会离开ConstraintLayout(父级)的一半 你可以想象这是因为我在LinearLayout 中设置了负边距

我拥有的是一个图像,它应该像图片一样被剪切,所以只有图像的按钮侧应该显示在实际设备上。其他部分应该剪掉。

这是我的布局的一部分。

<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="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:layout_width="71dp"
        android:layout_height="71dp"
        android:src="@drawable/someImage"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

那么,有什么好的方法吗?

【问题讨论】:

标签: android android-layout material-design android-view android-constraintlayout


【解决方案1】:

所以我找到了解决方案。 基本上你需要从它的容器中翻译图像。

android:translationY="-22dp"

【讨论】:

    【解决方案2】:

    添加一条指导线并说您的 ImageView 应该高于该指导线,例如此代码将使所有内容看起来像您的布局:

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        android:orientation="vertical">
    
       <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/your_image"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="@id/guideline" />
    
        <android.support.constraint.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_begin="163dp" />
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      【解决方案3】:

      要将ImageView 定位在父级之外的一半,通过将layout_height 设置为0dp 来强制执行垂直约束。要保持ImageView 的适当大小,请将dimensionRatio 设置为1:1。代码将如下所示(注意父 ConstraintLayout 现在有 layout_height 匹配父):

      <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:layout_width="71dp"
              android:layout_height="0dp"
              android:src="@drawable/someImage"
              app:layout_constraintBottom_toTopOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintDimensionRatio="1:1"/>
      </android.support.constraint.ConstraintLayout>
      

      【讨论】:

        【解决方案4】:

        将图像放入线性布局中。

        <LinearLayout....>
        
            <ImageView..../>
        
        </LinearLayout>
        

        并添加名为 clipChildren 的属性并使其 true

        【讨论】:

          【解决方案5】:

          一个技巧是在ConstraintLayout 本身中为您想要的一侧设置负边距。这要求对该侧有约束的其他视图进行偏移。图片中的右键在ConstraintLayout下方,隐藏在底栏下方:

          <?xml version="1.0" encoding="utf-8"?>
          <androidx.constraintlayout.widget.ConstraintLayout
              ...
              android:layout_marginBottom="-48dp">
          
              <ImageButton
                  android:id="@+id/leftButton"
                  android:layout_width="48dp"
                  android:layout_height="48dp"
                  android:layout_marginEnd="24dp"
                  android:layout_marginBottom="72dp"
                  android:background="@drawable/shape_next_button"
                  app:layout_constraintBottom_toBottomOf="parent"
                  app:layout_constraintEnd_toEndOf="parent" />
          
              <ImageButton
                  android:id="@+id/rightButton"
                  android:layout_width="48dp"
                  android:layout_height="48dp"
                  android:layout_marginStart="24dp"
                  android:background="@drawable/shape_previous_button"
                  app:layout_constraintBottom_toBottomOf="parent"
                  app:layout_constraintStart_toStartOf="parent" />
          
          </androidx.constraintlayout.widget.ConstraintLayout>
          

          【讨论】:

            【解决方案6】:

            我通过在 ConstraintLayout 中添加 View 并给它一些边距来做到这一点。

            View 为 ConstraintLayout 提供背景。

            ConstraintLayout 必须是透明背景。

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/bg_transparent">
            
                <View
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_marginTop="38dp"
                    android:background="@drawable/bg_white"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            
                <ImageView/>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2020-03-22
              • 2023-04-07
              • 1970-01-01
              • 2017-10-09
              • 2021-05-28
              • 1970-01-01
              • 2019-05-04
              • 2019-05-10
              相关资源
              最近更新 更多