代码之家  ›  专栏  ›  技术社区  ›  sir-haver

是否可以在一个线性布局中约束两个小部件?

  •  0
  • sir-haver  · 技术社区  · 5 年前

    enter image description here

    enter image description here

    我更喜欢使用线性布局,这样图像将彼此独立(稍后它们将可以移动)。

    我当前的XML:

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activities.EditProfileActivity">
    
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            app:cardBackgroundColor="@color/grey"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">
    
                <ImageView
                    android:id="@+id/profilePic1"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:scaleType="centerCrop"
                    app:srcCompat="@drawable/profile_pic1">
                </ImageView>
                <Button
                    android:id="@+id/deletePic1"
                    android:layout_width="@dimen/delete_picture_button_size"
                    android:layout_height="@dimen/delete_picture_button_size"
                    android:background="@drawable/delete_button"
                    android:translationX="5dp"
                    android:translationY="5dp"
                    app:layout_constraintBottom_toBottomOf="@+id/profilePic1"
                    app:layout_constraintEnd_toEndOf="@+id/profilePic1" />
    
    
            </LinearLayout>
    
    
        </androidx.cardview.widget.CardView>
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   Umesh AHIR    5 年前

    可以使用CardView内部的ConstraintLayout来执行此操作:

            <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <ImageView
                android:id="@+id/profilePic1"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:scaleType="centerCrop"
                app:layout_constraintEnd_toStartOf="@+id/profilePic2"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_person" />
    
            <androidx.appcompat.widget.AppCompatImageButton
                android:id="@+id/deletePic1"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="@drawable/style_white_cornar"
                android:translationX="5dp"
                android:translationY="5dp"
                app:layout_constraintBottom_toBottomOf="@+id/profilePic1"
                app:layout_constraintEnd_toEndOf="@+id/profilePic1" />
    
            <ImageView
                android:id="@+id/profilePic2"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:scaleType="centerCrop"
                app:layout_constraintEnd_toStartOf="@+id/profilePic3"
                app:layout_constraintStart_toEndOf="@+id/profilePic1"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_person" />
    
            <androidx.appcompat.widget.AppCompatImageButton
                android:id="@+id/deletePic2"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="@drawable/style_white_cornar"
                android:translationX="5dp"
                android:translationY="5dp"
                app:layout_constraintBottom_toBottomOf="@+id/profilePic2"
                app:layout_constraintEnd_toEndOf="@+id/profilePic2" />
    
            <ImageView
                android:id="@+id/profilePic3"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:scaleType="centerCrop"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/profilePic2"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_person" />
    
            <androidx.appcompat.widget.AppCompatImageButton
                android:id="@+id/deletePic3"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="@drawable/style_white_cornar"
                android:translationX="5dp"
                android:translationY="5dp"
                app:layout_constraintBottom_toBottomOf="@+id/profilePic3"
                app:layout_constraintEnd_toEndOf="@+id/profilePic3" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
        2
  •  1
  •   Niraj user1506104    5 年前

    <androidx.constraintlayout.widget.ConstraintLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/profilePic1"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:scaleType="centerCrop"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/square_pipe_icon" />
    
        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/deletePic1"
            android:layout_width="@dimen/delete_picture_button_size"
            android:layout_height="@dimen/delete_picture_button_size"
            android:background="@drawable/delete_button"
            android:translationX="5dp"
            android:translationY="5dp"
            app:layout_constraintBottom_toBottomOf="@+id/profilePic1"
            app:layout_constraintEnd_toEndOf="@+id/profilePic1" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    
        3
  •  1
  •   ssynhtn    5 年前

    使用 FrameLayout LinearLayout

    像这样做

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
            <ImageView
                android:layout_width="120dp"
                android:layout_height="120dp"
                />
    
            <Button
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_gravity="bottom|end"
                />
    
        </FrameLayout>