ImageView scalarea problemă în ConstriantLayout

voturi
0

Sunt folosind un ImageView într-un aspect de constrângere și doriți lățimea ImageView să fie lățimea și înălțimea ecranului de a fi dinamic, în funcție de imagine.

XML meu pentru layout este

<?xml version=1.0 encoding=utf-8?>
<android.support.v7.widget.CardView 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
style=@style/CardView.Light
android:layout_width=match_parent
android:layout_height=wrap_content
android:layout_marginTop=8dp>

<android.support.constraint.ConstraintLayout
    android:layout_width=match_parent
    android:layout_height=wrap_content
    android:background=@color/newsItemBackground
    tools:layout_editor_absoluteY=29dp>

    <TextView
        android:id=@+id/textTitle
        android:layout_width=0dp
        android:layout_height=wrap_content
        android:layout_marginEnd=8dp
        android:layout_marginLeft=8dp
        android:layout_marginRight=8dp
        android:layout_marginStart=8dp
        android:layout_marginTop=8dp
        android:textColor=@color/textHighEmphasis
        android:textSize=@dimen/news_title_size
        android:textStyle=bold
        app:layout_constraintEnd_toEndOf=parent
        app:layout_constraintStart_toStartOf=parent
        app:layout_constraintTop_toTopOf=parent
        tools:text=Title />

    <TextView
        android:id=@+id/textTime
        android:layout_width=0dp
        android:layout_height=wrap_content
        android:layout_marginEnd=8dp
        android:layout_marginLeft=8dp
        android:layout_marginRight=8dp
        android:layout_marginStart=8dp
        android:layout_marginTop=8dp
        android:fontFamily=@font/english_font
        android:textColor=@color/textMediumEmphasis
        android:textSize=@dimen/news_time_size
        android:textStyle=normal
        app:layout_constraintEnd_toEndOf=parent
        app:layout_constraintStart_toStartOf=parent
        app:layout_constraintTop_toBottomOf=@+id/textTitle
        tools:text=Time />

    <Button
        android:id=@+id/buttonMore
        style=@style/Widget.AppCompat.Button.Borderless.Colored
        android:layout_width=wrap_content
        android:layout_height=wrap_content
        android:layout_marginEnd=8dp
        android:layout_marginRight=8dp
        app:layout_constraintBottom_toBottomOf=parent
        app:layout_constraintEnd_toStartOf=@+id/buttonShare
        app:layout_constraintTop_toBottomOf=@+id/viewSeperator
        app:layout_constraintVertical_bias=0.0
        tools:text=readmore />

    <Button
        android:id=@+id/buttonShare
        style=@style/Widget.AppCompat.Button.Borderless.Colored
        android:layout_width=wrap_content
        android:layout_height=wrap_content
        android:layout_marginEnd=8dp
        android:layout_marginRight=8dp
        android:text=@string/share_button
        app:layout_constraintBottom_toBottomOf=parent
        app:layout_constraintEnd_toEndOf=parent
        app:layout_constraintTop_toBottomOf=@+id/viewSeperator
        app:layout_constraintVertical_bias=0.0 />


    <TextView
        android:id=@+id/textBody
        android:layout_width=0dp
        android:layout_height=wrap_content
        android:layout_marginEnd=8dp
        android:layout_marginLeft=8dp
        android:layout_marginRight=8dp
        android:layout_marginStart=8dp
        android:layout_marginTop=8dp
        android:textColor=@color/textHighEmphasis
        android:textSize=@dimen/news_body_size
        android:textStyle=normal
        app:layout_constraintEnd_toEndOf=parent
        app:layout_constraintHorizontal_bias=0.0
        app:layout_constraintStart_toStartOf=parent
        app:layout_constraintTop_toBottomOf=@+id/imageNews
        tools:text=Body />


    <View
        android:id=@+id/viewSeperator
        android:layout_width=0dp
        android:layout_height=1px
        android:layout_marginEnd=8dp
        android:layout_marginLeft=8dp
        android:layout_marginRight=8dp
        android:layout_marginStart=8dp
        android:layout_marginTop=8dp
        android:background=@color/cardSeperatorLine
        app:layout_constraintEnd_toEndOf=parent
        app:layout_constraintStart_toStartOf=parent
        app:layout_constraintTop_toBottomOf=@+id/textBody />

    <ImageView
        android:id=@+id/imageNews
        android:layout_width=0dp
        android:layout_height=wrap_content
        android:layout_marginEnd=8dp
        android:layout_marginLeft=8dp
        android:layout_marginRight=8dp
        android:layout_marginStart=8dp
        android:layout_marginTop=8dp
        android:scaleType=centerCrop
        app:layout_constraintEnd_toEndOf=parent
        app:layout_constraintHorizontal_bias=0.0
        app:layout_constraintStart_toStartOf=parent
        app:layout_constraintTop_toBottomOf=@+id/textTime
        app:srcCompat=@drawable/portait_man />


</android.support.constraint.ConstraintLayout>

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

Această imagine arată bine în editorul de studio Android cu toate acestea, atunci când am încărcat-folosind Glide, înălțimea imaginii este decupată de sus și de jos.

Imagine de la Editor este

Editor

Image în timp ce rulează aplicația

Imagine

Sunt de încărcare utilizând codul

Glide.with(imageNews.getContext())
                .load(item.image_url)
                .into(imageNews);
Întrebat 02/09/2018 la 05:23
sursa de către utilizator
În alte limbi...                            


3 răspunsuri

voturi
0

Trebuie doar să setați

android:adjustViewBounds="true"

și va funcționa

Publicat 02/09/2018 la 06:05
sursa de către utilizator

voturi
0

Încercați mai jos

<android.support.v7.widget.CardView 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="wrap_content"
    android:layout_marginTop="8dp">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Title" />

        <TextView
            android:id="@+id/textTime"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:textStyle="normal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textTitle"
            tools:text="Time" />

        <Button
            android:id="@+id/buttonMore"
            style="@style/Widget.AppCompat.Button.Borderless.Colored"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/buttonShare"
            app:layout_constraintTop_toBottomOf="@+id/viewSeperator"
            app:layout_constraintVertical_bias="0.0"
            tools:text="readmore" />

        <Button
            android:id="@+id/buttonShare"
            style="@style/Widget.AppCompat.Button.Borderless.Colored"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:text="share_button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/viewSeperator"
            app:layout_constraintVertical_bias="0.0" />


        <TextView
            android:id="@+id/textBody"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:textStyle="normal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageNews"
            tools:text="Body" />


        <View
            android:id="@+id/viewSeperator"
            android:layout_width="0dp"
            android:layout_height="1px"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textBody" />

        <ImageView
            android:id="@+id/imageNews"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textTime"
            app:srcCompat="@mipmap/ic_logo" />


    </android.support.constraint.ConstraintLayout>

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

Asigurați-vă că temporar am elimina doar codul de culoare și text din șir.

Publicat 02/09/2018 la 05:32
sursa de către utilizator

voturi
0

Rețineți că centerCroppoate mări oarecum imaginea un pic. Dacă doriți ImageViewsă fie flexibil , pe baza imaginii de rezoluție de eliminare , scaleTypedar acest lucru poate duce la un comportament neașteptat.

<ImageView
        android:id="@+id/imageNews"
        android:layout_width="wrap_content" // Changed to wrap_content
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textTime"
        app:srcCompat="@drawable/portait_man" />
Publicat 02/09/2018 la 05:28
sursa de către utilizator

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more