Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to offset a view from center?

Tags:

I'm trying to possition my button not exactly in center, but let's say in the 2/5s of the screen height, I was looking for attribute unsuccessfully, so I tried this approach

<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" android:background="@drawable/background" android:padding="20dp" >   <ImageButton     android:id="@+id/flashlight_button"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_centerHorizontal="true"     android:layout_above="@+id/fakeView"     android:background="@null"     android:contentDescription="@string/flashlight_button_description"     android:src="@drawable/freeml_bright" />     <View     android:id="@+id/fakeView"     android:layout_width="10dp"     android:layout_height="10dp"     android:layout_centerInParent="true"     android:background="#FFAABB" />  </RelativeLayout> 

However it does not work, even if I set margin on the fake view.

Padding attribute works, however as it is a big image and if I want it to start at 2/5ths of screen height, it covers the center point of screen, so if I use padding attribute it works but it pushes it away from center and does not allow it to cover it.

Alhough, I made it work using LinearLayout, which I wanted to avoid because there are more Views on top and bottom next to each other so it would lead to nested views using linear layout. Unfortunately I think its the only option.

It basically it uses another linear layout that fills the remaining space left unused by top and bottom views with height=0dp and weight=1 and sets its gravity to center.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="20dp">  <LinearLayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="horizontal"     android:gravity="center" >      <ImageView         android:id="@+id/imageView1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:contentDescription="@string/application_logo_description"         android:src="@drawable/mylight" />       <ImageButton         android:id="@+id/settings_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="right"         android:background="@null"         android:contentDescription="@string/settings_button_description"         android:src="@drawable/settings_button" />  </LinearLayout>  <LinearLayout     android:layout_width="match_parent"     android:layout_height="0dp"     android:layout_weight="1"     android:gravity="center"     android:orientation="vertical" >      <ImageButton         android:id="@+id/flashlight_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="@null"         android:contentDescription="@string/flashlight_button_description"         android:src="@drawable/flashlight_button_selector" />      <View         android:id="@+id/fakeView"         android:layout_width="0dp"         android:layout_height="0dp"         android:layout_marginTop="60dp"         android:background="#FFAABB" /> </LinearLayout>  <ImageView     android:id="@+id/imageView2"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_gravity="bottom|center_horizontal"     android:contentDescription="@string/powered_by_description"     android:src="@drawable/powered_by" />  <ImageButton     android:id="@+id/ad_button"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_gravity="bottom|center_horizontal"     android:background="@null"     android:contentDescription="@string/ad_button_description"     android:src="@drawable/freeml" />   </LinearLayout> 
like image 585
urSus Avatar asked Jul 27 '12 14:07

urSus


People also ask

How do you center a view?

You can apply a centering to any View, including a Layout, by using the XML attribute android:layout_gravity". You probably want to give it the value "center". AFAIK, layout_gravity is only for LinearLayout, not for arbitrary layouts.

How do I center a view in relative layout?

Below that, the layout_height=0 and layout_weight=1 attributes on the RelativeLayout cause it to take up all the remaining space. You can then center the button in the RelativeLayout . You can play with padding on the button to get it to the size you want.

What is Android offset?

Generally speaking, Offsets can be interpreted in two ways: As representing a point in Cartesian space a specified distance from a separately-maintained origin. For example, the top-left position of children in the RenderBox protocol is typically represented as an Offset from the top left of the parent box.


1 Answers

Depending on the screen size of the target device, padding by X dp might move it more than just a fifth of the height.

You might have to code this movement yourself. However, nothing prevents you from doing:

<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"  android:background="@drawable/background"  android:padding="20dp" >  <View android:id="@+id/fakeView" android:layout_width="10dp" android:layout_height="10dp" android:layout_centerInParent="true" android:background="#FFAABB" />  <ImageButton android:id="@+id/flashlight_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_above="@+id/fakeView" android:marginBottom="50dp" android:background="@null" android:contentDescription="@string/flashlight_button_description" android:src="@drawable/freeml_bright" />   </RelativeLayout> 
like image 160
Shark Avatar answered Oct 15 '22 10:10

Shark