25

I have following layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/test_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/main_layPanelCircle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="15dp"
    android:background="@drawable/main_panel_a"
    android:orientation="vertical"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/main_imgBreachAlert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp" />

    <Button
        android:id="@+id/main_btnMap"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@null"
        android:drawableTop="@drawable/icon_find"
        android:text="@string/main_locate"
        android:textColor="@android:color/white"
        android:textSize="10sp" />

    <LinearLayout
        android:id="@+id/main_llPanelCycleMiddle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/main_btnMap"
        android:layout_centerInParent="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/main_btnLeft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:drawableTop="@drawable/icon_call"
            android:text="@string/main_call"
            android:textColor="@android:color/white"
            android:textSize="10sp"
            android:visibility="visible" />

        <Button
            android:id="@+id/main_btnEmergency"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:drawableTop="@drawable/icon_emergency"
            android:scaleType="center"
            android:text="@string/main_emergency"
            android:textColor="@android:color/white"
            android:textSize="9sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/main_btnRight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:drawableTop="@drawable/icon_store"
            android:text="@string/main_store"
            android:textColor="@android:color/white"
            android:textSize="10sp" />
    </LinearLayout>

    <Button
        android:id="@+id/main_btnMore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/main_llPanelCycleMiddle"
        android:layout_centerHorizontal="true"
        android:background="@null"
        android:drawableTop="@drawable/icon_more"
        android:text="@string/main_more"
        android:textColor="@android:color/white"
        android:textSize="10sp" />
</RelativeLayout>

</LinearLayout>

When i set main_imgBreachAlert android:layout_alignParentLeft="true" eveerything is OK (left image) but when i set it to right mine relative layout stretches (left image):

https://i.stack.imgur.com/lojJX.jpg

Could you please advise how could I fix this problem.

Thnx

Onik
  • 19,396
  • 14
  • 68
  • 91
Damir
  • 1,092
  • 4
  • 18
  • 32
  • Are you aligning it to parent left and right at the same time? If so, that forces it to fill parent, overriding the wrap_content (as the lesser of two evils, the other option would be to shrink anything else in the layout to the same size). – Gabe Sechan Jul 03 '13 at 11:06

1 Answers1

57

Try android:layout_centerInParent="true" and remove the alignParentLeft attribute if you want the image centered in your layout.

If you only want to align it to the right, just use android:layout_alignParentRight="true".

<ImageView
    android:id="@+id/main_imgBreachAlert"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="5dp" />
Pang
  • 9,564
  • 146
  • 81
  • 122
Flynn81
  • 4,108
  • 29
  • 28
  • 1
    Then only use android:alignParentRight="true" and remove the alignParentLeft attribute. – Flynn81 Jul 03 '13 at 12:21
  • 2
    If I set alignParentRight than it stretches parent in which this control is in. I thin that my approach is wrong. I actual want to set layout background and wrap layout around that background and than put other controls inside that layout but it should stay wrapped around background image. If this makes any sense. – Damir Jul 03 '13 at 14:04
  • If you try the code from my answer it stretches? It should just wrap the content (your image) and align it right. – Flynn81 Jul 03 '13 at 15:18