Reputation: 7304
My problem is may be trivial for other people, but for me I can't make it work. I have layout xml as follow.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout android:id="@+id/tracker_container"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical"
/>
<ImageView
android:id="@+id/mainImageView"
android:contentDescription="@string/display"
android:layout_gravity="center"
android:layout_width="400dp"
android:layout_height="300dp"
android:opacity="translucent"
/>
</LinearLayout>
How can I make the ImageView
is at the center of the whole screen?
Thanks
Upvotes: 0
Views: 3512
Reputation: 893
Try this--
<?xml version="1.0" encoding="utf-8"?>
<RelativeLaout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout android:id="@+id/tracker_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<ImageView
android:id="@+id/mainImageView"
android:contentDescription="@string/display"
android:layout_centerInParent="true"
android:layout_width="400dp"
android:layout_height="300dp"
android:opacity="translucent"
/>
</RelativeLayout>
Upvotes: 1
Reputation: 23269
You can use a RelativeLayout
as the parent and use layout_centerInParent
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/tracker_container"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" />
<ImageView
android:id="@+id/mainImageView"
android:contentDescription="@string/display"
android:layout_centerInParent="true"
android:layout_width="400dp"
android:layout_height="300dp"
android:opacity="translucent" />
</RelativeLayout>
I'm not sure what that LinearLayout
is for but I have kept it in there. Perhaps you are populating it programmatically and it's pushing your ImageView
down. RelativeLayout
ensures it will always be central to the parent, regardless of other view elements.
Upvotes: 0