问题
I am trying to add some Images by ImageView in Android Activity(ConstraintLayout), on Android Studio Preview screen, it's showing images correctly, but when I am running the app on real device or emulator, it's not showing the image.
Below is the code which I am using,
<?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"
tools:context="com.vchamps.android.staffast.ProfessionalsCalc"
android:background="@color/colorPrimary">
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guidelineVertical1"
app:layout_constraintGuide_percent="0.1"
android:orientation="vertical"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="39dp" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guidelineVertical2"
app:layout_constraintGuide_percent="0.25"
android:orientation="vertical"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="96dp" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guidelineHorizontal"
app:layout_constraintGuide_begin="87dp"
android:orientation="horizontal"
tools:layout_editor_absoluteY="87dp"
tools:layout_editor_absoluteX="0dp" />
<ImageView
android:visibility="visible"
android:id="@+id/imgRate"
android:layout_width="@dimen/rates_icon_size"
android:layout_height="@dimen/rates_icon_size"
app:srcCompat="@drawable/icon_rates"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
app:layout_constraintTop_toTopOf="@+id/guidelineHorizontal"
android:layout_marginTop="27dp"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="@+id/guidelineVertical1"
/>
<ImageView
android:visibility="visible"
android:id="@+id/imgSchedule"
android:layout_width="@dimen/rates_icon_size"
android:layout_height="@dimen/rates_icon_size"
app:srcCompat="@drawable/icon_schedule"
android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="@+id/imgRate"
app:layout_constraintLeft_toLeftOf="@+id/guidelineVertical1"
android:scaleType="fitCenter"
/>
<ImageView
android:visibility="visible"
android:id="@+id/imgPayout"
android:layout_width="36dp"
android:layout_height="36dp"
app:layout_constraintLeft_toLeftOf="@+id/guidelineVertical1"
app:srcCompat="@drawable/icon_payout"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/imgSchedule"
android:scaleType="fitCenter"
/>
<TextView
android:id="@+id/tvRate"
style="@style/fontRateScreenTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:text="Industry Leading Rates"
app:layout_constraintLeft_toLeftOf="@+id/guidelineVertical2"
app:layout_constraintTop_toTopOf="@+id/guidelineHorizontal" />
<TextView
android:id="@+id/tvSchedule"
style="@style/fontRateScreenTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="Flexible Schedule"
app:layout_constraintLeft_toLeftOf="@+id/guidelineVertical2"
app:layout_constraintTop_toTopOf="@+id/tvRate" />
<TextView
android:id="@+id/tvPayout"
style="@style/fontRateScreenTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="Quick and Easy Payout"
app:layout_constraintLeft_toLeftOf="@+id/guidelineVertical2"
app:layout_constraintTop_toTopOf="@+id/tvSchedule" />
<Button
android:id="@+id/btnWork"
style="@style/ButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="CLICK ME"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
</android.support.constraint.ConstraintLayout>
The UI Preview in Android Studio is as below,
We can see that 3 Images are getting displayed here,
But when I run the application on emulator or on real device, it's not showing any of 3 images. Below is the screenshot of the emulator,
- Value of @dimen/rates_icon_size: 36dp
- Three Images Dimensions,
- icon_rates : 87x59 Pixels
- icon_schedule : 88x78 Pixels
- icon_payout : 60x82 Pixels
Please help me by providing resolution of the query, Thanks in advance.
回答1:
In ImageView instead of using srcCompact use src
<ImageView
android:visibility="visible"
android:id="@+id/imgRate"
android:layout_width="@dimen/rates_icon_size"
android:layout_height="@dimen/rates_icon_size"
app:src="@drawable/icon_rates"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
app:layout_constraintTop_toTopOf="@+id/guidelineHorizontal"
android:layout_marginTop="27dp"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="@+id/guidelineVertical1"
/>
instead of
<ImageView
android:visibility="visible"
android:id="@+id/imgRate"
android:layout_width="@dimen/rates_icon_size"
android:layout_height="@dimen/rates_icon_size"
app:srcCompat="@drawable/icon_rates"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
app:layout_constraintTop_toTopOf="@+id/guidelineHorizontal"
android:layout_marginTop="27dp"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="@+id/guidelineVertical1"
/>
回答2:
use android:src="@drawable/icon_rates"
sometime app:srcCompat="@drawable/icon_rates"
not work.
<ImageView
android:visibility="visible"
android:id="@+id/imgRate"
android:layout_width="@dimen/rates_icon_size"
android:layout_height="@dimen/rates_icon_size"
android:src="@drawable/icon_rates"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
app:layout_constraintTop_toTopOf="@+id/guidelineHorizontal"
android:layout_marginTop="27dp"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="@+id/guidelineVertical1"
/>
回答3:
I think your images are the problem. At least it's not a elegant solution.
If your app should run on different devices then you should use Android Vector Drawables instead of normal PNG images. Using vector drawables instead of bitmaps reduces the size of your APK because the same file can be resized for different screen densities without loss of image quality.
If you want to use your images then you should convert them first in the SVG format (with Inkscape for example it's for free) and then in the Vector Drawable format with this free online tool.
The second solution would be to use Material Design Icons. They are developed with many designers to ensure a good user experience and they are also available in the vector drawable format.
I tryed your code out with the vector drawables and the app is displaying the images without problems.
来源:https://stackoverflow.com/questions/43715050/constraint-layout-not-showing-image-from-imageview-when-app-run