My ImageView for some odd reason is always displaying a picture in the middle, left of my screen despite the xml code insisting it to be in the center.
I force portrait orientation in my app and here is my xml code for my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/image_view"
android:layout_weight="1.0"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/message"
android:gravity="center_horizontal"
android:padding="10dip"/>
<Button
android:layout_gravity="center_horizontal"
android:layout_width="fill_parent"
android:textStyle="bold" android:id="@+id/action_button" android:selectAllOnFocus="false" android:layout_height="100dip" android:text="Click here to preform actions"/>
</LinearLayout>
So, the page should display as Picture in the middle, extending the width of the screen, the text below that and the button below that. But for some reason, the picture is displayed as a small, box thumbnail in the middle left of the screen – any ideas on a workaround?
Assuming you want your picture centered inside a top box here, you don’t want
android:gravity="center", you actually just wantandroid:scaleTypeset on theImageViewitself. “center” is the right value if you don’t want scaling;centerInsideis probably appropriate if you do (in which case you’ll need to define some sort of dimensions or weight on yourImageView). For more on scaleType values, see the documentation.You also don’t want the image height set to fill_parent, or it will do that (which means no text below it, since the image fills the entire parent
LinearLayoutleaving no room for text). You probably want theImageView‘s height set towrap_contentor some fixed height (e.g.100dip).