【问题标题】:Move ImageView inside CardView在 CardView 中移动 ImageView
【发布时间】:2019-11-16 09:21:25
【问题描述】:

我正在尝试在 CardView 内移动 ImageView。我有一个带有一些文本和一个 ImageView 的 CardView,我想将我的圆形 ImageView 放在中心的文本上方。

在图片中你可以看到 CardView 左上角的绿色可绘制圆圈,并且 TextView 居中。 ImageView stuck at top left of CardView

这是我当前的代码:

<android.support.constraint.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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomePageActivity"
    style="@android:style/TextAppearance.DeviceDefault.Medium">

...在我拥有的 ConstraintLayout 内部:

<android.support.v7.widget.CardView
    android:id="@+id/create_invoice_cardview"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="@dimen/button_gap_normal"
    android:layout_marginTop="185dp"
    android:layout_marginEnd="@dimen/margin_width_normal"
    android:layout_marginBottom="@dimen/button_gap_normal"
    app:layout_constraintBottom_toTopOf="@+id/add_single_cardview"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/capture_receipt_cardview"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/inset_background"
        android:padding="10dp"
        android:src="@drawable/ic_photo_camera_black_24dp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:paddingTop="25dp"
        android:text="Create Invoice"
        android:autoSizeTextType="uniform"
        android:autoSizeMaxTextSize="25sp"
        />

</android.support.v7.widget.CardView>

我曾尝试在 inset_background 可绘制对象中使用 inset,但它只是将圆形挤压成椭圆形而不移动它。

如何在 CardView 内移动 ImageView,同时保持可绘制形状为圆形?

我希望此解决方案在我修改手机屏幕大小时相应地调整大小。即我不想要一个在 Pixel XL 上小而在 Nexus 4 上大的硬编码尺寸。

为了更好地了解我在寻找什么,this is what I am trying to design

【问题讨论】:

    标签: android android-studio imageview cardview


    【解决方案1】:

    将您的 ImageViewTextView 包裹在 LinearLayout 中,如下所示:

    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="match_parent"
            tools:context=".MainActivity">
    
            <ImageView
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:drawable/arrow_up_float"
                    android:padding="10dp"/>
    
            <TextView
                    android:id="@+id/textView3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:paddingTop="25dp"
                    android:text="Create Invoice"
                    android:autoSizeTextType="uniform"
                    android:autoSizeMaxTextSize="25sp"/>
    </LinearLayout>
    

    基本上,此 LinearLayout 将在彼此下方对齐项目。通过将gravity center 设置为ImageView,它将正确居中。由于它是在 ImageView 之前声明的,所以它会出现在它的上方。

    【讨论】:

    • 在此解决方案中,ImageView 的大小基于可绘制对象的大小(高度和宽度的包装内容)。如何更改此设置,使可绘制对象不是固定值,而是随屏幕大小缩放?
    【解决方案2】:

     <android.support.v7.widget.CardView
            android:id="@+id/create_invoice_cardview"
            app:layout_constraintBottom_toTopOf="@+id/add_single_cardview"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/capture_receipt_cardview"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="@dimen/button_gap_normal"
            android:layout_marginTop="185dp"
            android:layout_marginEnd="@dimen/margin_width_normal"
            android:layout_marginBottom="@dimen/button_gap_normal">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/inset_background"
                    android:centerHorizontal="true"
                    android:padding="10dp"
                    android:src="@drawable/ic_photo_camera_black_24dp" />
    
                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:autoSizeMaxTextSize="25sp"
                    android:autoSizeTextType="uniform"
                    android:centerInParent="true"
                    android:paddingTop="25dp"
                    android:text="Create Invoice" />
    
            </RelativeLayout>
        </android.support.v7.widget.CardView>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-23
      • 2020-09-04
      • 2018-08-11
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      相关资源
      最近更新 更多