简体   繁体   中英

Align Bitmap Image inside ImageView - CS50 Pokedex Android

I have been working on CS50 and now I am tracking android. I almost finished my PSet Pokedex, but small thing is bothering me.

I am trying to align bitmap image on top of ImageView, but I could not do it. Could you please help me? I tried "scaletype", "layout_gravitiy" etc. But it does not seem to work. I want image to be just below description stating that this should be changed by the java code. Please see current view here

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".PokemonActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pokemon_name"
        android:textAlignment="center"
        android:textSize="18dp"
        android:textStyle="bold"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pokemon_number"
        android:textAlignment="center"
        android:textSize="16dp"
        android:paddingTop="5dp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pokemon_type1"
        android:textAlignment="center"
        android:textSize="16dp"
        android:paddingTop="5dp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pokemon_type2"
        android:textAlignment="center"
        android:textSize="16dp"
        android:paddingTop="5dp" />

    <Button
        android:id="@+id/pokemon_button_catch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:paddingTop="5dp"
        android:textSize="24dp"
        android:onClick="toggleCatch"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pokemon_description"
        android:textAlignment="center"
        android:textSize="16dp"
        android:textStyle="bold|italic"
        android:paddingTop="50dp"
        android:text="This is testing something, this should changed by the java code."/>

    <ImageView
        android:id="@+id/pokemon_avatar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Welcome to StackOverFlow !

If there is no special requirement than no need to use match_parent inside ImageView. You can do following adjustment-

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".PokemonActivity">

    ..............

    <ImageView
        android:id="@+id/pokemon_avatar"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

Happy coding !

sorry. I was roasting snow in a furnace. I found out that actually source image is one to blame. Image size does not wrap the content but it has extra space around content. This is the reason when it is enlarged, there is space between text and image. 图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM