Imageview skalering problem i ConstriantLayout

stemmer
0

Jeg bruker et bildeVis i en begrensning utforming og vil bredden av Imageview å være skjermens bredde og høyde for å være dynamisk i henhold til bilde.

Min XML for layout er

<?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>

Dette bildet ser fint i android studio redaktør men når jeg legger det ved hjelp Glide, er bildets høyde beskjæres fra topp og bunn.

Bilde fra redaktøren er

redaktør

Bilde mens du kjører app

levende

Jeg legger i å bruke koden

Glide.with(imageNews.getContext())
                .load(item.image_url)
                .into(imageNews);
Publisert på 02/09/2018 klokken 05:23
kilden bruker
På andre språk...                            


3 svar

stemmer
0

bare sett

android:adjustViewBounds="true"

og det vil fungere

Svarte 02/09/2018 kl. 06:05
kilden bruker

stemmer
0

Prøv nedenfor

<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>

Sørg for at midlertidig jeg har bare fjerne din fargekode og tekst fra streng.

Svarte 02/09/2018 kl. 05:32
kilden bruker

stemmer
0

Husk at centerCropkan noe zoome bildet litt. Hvis du vil at ImageViewskal være fleksibel basert på bildets oppløsning fjerne scaleType, men dette kan føre til uventet oppførsel.

<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" />
Svarte 02/09/2018 kl. 05:28
kilden bruker

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