【问题标题】:View between two views android XML在两个视图之间查看android XML
【发布时间】:2021-11-14 06:13:51
【问题描述】:

我需要在卡片和活动背景之间显示图像。附上示例图像。到目前为止,我一直在为布局而苦苦挣扎。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns: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"
android:background="#D3E6ED"
tools:context=".MainActivity">


<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="10dp"
    android:layout_marginTop="120dp"
    android:layout_marginEnd="10dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:cardCornerRadius="10dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

图片已附上。

【问题讨论】:

    标签: android kotlin android-layout user-interface android-constraintlayout


    【解决方案1】:

    将此库用于圆形图像。此代码将解决您的问题。

    implementation 'de.hdodenhof:circleimageview:2.2.0'
    

    布局:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/tools"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f36121">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="20dp"
            app:layout_constraintTop_toTopOf="parent">
    
            <androidx.cardview.widget.CardView
                android:id="@+id/card_login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="50dp"
                android:foreground="?android:attr/selectableItemBackground"
                card_view:cardBackgroundColor="#ffffff"
                card_view:cardCornerRadius="4dp"
                card_view:cardElevation="0dp"
                card_view:cardUseCompatPadding="false">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="50dp"
                    android:orientation="vertical"
                    android:padding="24dp">
    
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Login" />
    
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="16dp"
                        android:hint="Password"
                        android:inputType="textPassword" />
    
                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="16dp"
                        android:background="@android:color/holo_blue_dark"
                        android:text="Sign In" />
    
                </LinearLayout>
            </androidx.cardview.widget.CardView>
            
            <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/profile_image"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:layout_alignParentTop="true"
                android:layout_centerInParent="true"
                android:layout_marginEnd="@dimen/_5sdp"
                android:src="@mipmap/template_icon"
                app:civ_border_color="#f36121"
                app:civ_border_width="10dp"
                app:layout_constraintBottom_toTopOf="@+id/card_login"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
        </RelativeLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      【解决方案2】:

      将卡片和图像视图放在同一位置,但在卡片上添加等于图像视图高度一半的上边距。并将图像视图放在 xml 中的第二位,以便它在 z 顺序中位于顶部。

      【讨论】:

        【解决方案3】:

        这是一些接近的输出。只需根据需要更改卡片视图形状即可。

        <?xml version="1.0" encoding="utf-8"?>
        <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"
            android:background="#D3E6ED">
        
            <androidx.cardview.widget.CardView
                android:id="@+id/cardView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="10dp"
                android:layout_marginTop="120dp"
                android:layout_marginEnd="10dp"
                app:cardCornerRadius="10dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        
            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/imageView"
                android:layout_width="@dimen/_100sdp"
                android:layout_height="@dimen/_100sdp"
                android:layout_marginTop="@dimen/_50sdp"
                android:translationZ="@dimen/_90sdp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:src="@drawable/current_location" />
        
            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/img_camera"
                android:layout_width="@dimen/_20sdp"
                android:layout_height="@dimen/_20sdp"
                android:layout_marginEnd="@dimen/_5sdp"
                android:layout_marginBottom="@dimen/_15sdp"
                android:tint="@color/black"
                android:translationZ="@dimen/_90sdp"
                app:layout_constraintBottom_toBottomOf="@+id/imageView"
                app:layout_constraintEnd_toEndOf="@+id/imageView"
                tools:src="@drawable/ic_icon_camera_attendant" />
        
        </androidx.constraintlayout.widget.ConstraintLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多