在构建 android 应用程序时,ImageView 无法在 CardView 上运行
ImageView doesn't working on CardView on the build of an android app
我正在学习在 android studio 上为 android 开发应用程序,但遇到了 1 个问题。
我在 CardView 上添加了 ImageView。在预览时我可以看到 ImageView,但在构建时我看不到。如何解决?
XML代码:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.51">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/cardView2"
android:layout_width="393dp"
android:layout_height="118dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/cardView"
app:layout_constraintTop_toBottomOf="@+id/cardView">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_marginEnd="220dp"
android:adjustViewBounds="true"
android:contentDescription="@string/da"
android:cropToPadding="true"
android:scaleType="fitXY"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/backgrounds/scenic[15]" />
<TextView
android:id="@+id/textView9"
android:layout_width="210dp"
android:layout_height="99dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:fontFamily="serif"
android:text="@string/tri"
android:textColor="@android:color/background_dark"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.28"
app:layout_constraintStart_toEndOf="@+id/imageView4"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.65999997" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="385dp"
android:layout_height="134dp"
app:layout_constraintBottom_toTopOf="@+id/cardView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.48000002">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView8"
android:layout_width="249dp"
android:layout_height="109dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:fontFamily="serif"
android:text="@string/b"
android:textAlignment="viewStart"
android:textColor="@android:color/background_dark"
android:textSize="20sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="@+id/imageView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/imageView3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.666" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/da"
android:cropToPadding="true"
android:scaleType="fitXY"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textView8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars[0]" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Main_activity 为空 :D
我所期望的
https://cdn1.savepice.ru/uploads/2018/12/24/eae2df1528a0b3949d7daf1bf82132d1-full.jpg
我有什么
https://cdn1.savepice.ru/uploads/2018/12/24/8c78dd0e5a98d622a429a08bca14c8bb-full.jpg
抱歉,这是一个愚蠢的问题,但我在 google
找不到解决方案
做这个
tools:srcCompat="@tools:sample/backgrounds/scenic[15]"
为此:
app:srcCompat="@tools:sample/backgrounds/scenic[15]"
实际上,添加一个不同的可绘制对象,因为这些头像仅适用于示例版本,不适用于构建版本。
避免这种情况
AAPT:错误:“@tools:sample/avatars”与属性 srcCompat (attr) 引用不兼容 [weak]。
使用可绘制对象
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/imageView"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:srcCompat="@drawable/ic_logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
对您的图像视图执行此操作
android:src="@drawable/image"
将图片放入res/drawable
我正在学习在 android studio 上为 android 开发应用程序,但遇到了 1 个问题。 我在 CardView 上添加了 ImageView。在预览时我可以看到 ImageView,但在构建时我看不到。如何解决?
XML代码:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.51">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/cardView2"
android:layout_width="393dp"
android:layout_height="118dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/cardView"
app:layout_constraintTop_toBottomOf="@+id/cardView">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_marginEnd="220dp"
android:adjustViewBounds="true"
android:contentDescription="@string/da"
android:cropToPadding="true"
android:scaleType="fitXY"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/backgrounds/scenic[15]" />
<TextView
android:id="@+id/textView9"
android:layout_width="210dp"
android:layout_height="99dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:fontFamily="serif"
android:text="@string/tri"
android:textColor="@android:color/background_dark"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.28"
app:layout_constraintStart_toEndOf="@+id/imageView4"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.65999997" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="385dp"
android:layout_height="134dp"
app:layout_constraintBottom_toTopOf="@+id/cardView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.48000002">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView8"
android:layout_width="249dp"
android:layout_height="109dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:fontFamily="serif"
android:text="@string/b"
android:textAlignment="viewStart"
android:textColor="@android:color/background_dark"
android:textSize="20sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="@+id/imageView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/imageView3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.666" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/da"
android:cropToPadding="true"
android:scaleType="fitXY"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textView8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars[0]" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Main_activity 为空 :D
我所期望的 https://cdn1.savepice.ru/uploads/2018/12/24/eae2df1528a0b3949d7daf1bf82132d1-full.jpg
我有什么 https://cdn1.savepice.ru/uploads/2018/12/24/8c78dd0e5a98d622a429a08bca14c8bb-full.jpg
抱歉,这是一个愚蠢的问题,但我在 google
找不到解决方案做这个
tools:srcCompat="@tools:sample/backgrounds/scenic[15]"
为此:
app:srcCompat="@tools:sample/backgrounds/scenic[15]"
实际上,添加一个不同的可绘制对象,因为这些头像仅适用于示例版本,不适用于构建版本。
避免这种情况
AAPT:错误:“@tools:sample/avatars”与属性 srcCompat (attr) 引用不兼容 [weak]。
使用可绘制对象
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/imageView"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:srcCompat="@drawable/ic_logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
对您的图像视图执行此操作
android:src="@drawable/image"
将图片放入res/drawable