简体   繁体   中英

Unable to align ImageView to bottom of RelativeLayout

I've read several tutorials on how to do this, but for whatever reason, my image won't budge from the center of the screen. The ninjas image is 1080x517px

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:weightSum="1"
              android:background="@color/white">

    <ImageView
        android:id="@+id/ninjas"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/ninjas"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

From what I can tell, this is correct. Any suggestions? Thanks in advance

You make the image fill the parent currently. Change

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

to

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

try this

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >

<ImageView
    android:id="@+id/ninjas"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/ic_launcher" />

 </RelativeLayout>

Try changing

<ImageView
    android:id="@+id/ninjas"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/ninjas"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"/>

to

<ImageView
    android:id="@+id/ninjas"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ninjas"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"/>

At present your ImageView is occupying all the space in its parent. After changing layout_width and layout_height to wrap_content it will only occupy as much space as size of @drawable/ninjas

// Try this way,hope this will help you to solve your problem...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="bottom|right"
    android:padding="5dp">

    <ImageView
        android:id="@+id/ninjas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ninjas"
        android:adjustViewBounds="true"/>

</LinearLayout

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