0

I have this situation:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="fill_parent"  
android:layout_height="fill_parent"  
>  



<ImageButton android:id="@+id/btn1"  
 android:layout_width="60px"   
    android:layout_height="80px"  
    android:layout_marginLeft="50px"
    android:layout_marginTop="50px"
    android:clickable="true"  
    android:tag="1"  
    android:background="@drawable/xo" 
/>  



<ImageButton android:id="@+id/btn2"  
 android:layout_width="60px"   
    android:layout_height="80px"  
    android:layout_marginLeft="120px"
    android:layout_marginTop="50px" 
    android:clickable="true"  
    android:tag="2"  
    android:background="@drawable/xo" 
/>  

<ImageButton android:id="@+id/btn3"  
 android:layout_width="60px"   
    android:layout_height="80px"  
    android:layout_marginLeft="190px"
    android:layout_marginTop="50px"   
    android:clickable="true"  
    android:tag="3"  
    android:background="@drawable/xo" 
/>  
</RelativeLayout>

Can I use percents instead of 50px? If yes how is the syntax? I have this 3 buttons, and want to display in the middle of the page. I want to be one after the other, aligned horizontally.

Goo
  • 1,318
  • 1
  • 13
  • 31
Andreea
  • 151
  • 1
  • 3
  • 12

3 Answers3

0

I don't know if percent are allowed but I do know that px/pixels are generally frowned upon because of varying screen resolutions and densities.

http://developer.android.com/guide/topics/resources/more-resources.html#Dimension describes the dp (density-indepedent pixels) which might of use to you. Can't help you with the percent part though.

Lars Wikman
  • 366
  • 1
  • 5
0

No idea about percentage i haven't yet came across percentage ever . there are other measurements used in android . I would prefer you use dp or sp and never use pixels untill very necessary . Here is the link with difference between different scales.

What is the difference between "px", "dp", "dip" and "sp" on Android?

Community
  • 1
  • 1
0

You need the android:layout_centerVertical="true" , not percent of layout_marginTop.

I modify your xml for this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
>  
<ImageButton android:id="@+id/btn1"  
    android:layout_width="60dp"   
    android:layout_height="80dp"  
    android:layout_marginLeft="50dp"
    android:layout_centerVertical="true"
    android:clickable="true"  
    android:tag="1"  
    android:background="@android:color/darker_gray" 
/>  
<ImageButton android:id="@+id/btn2"  
    android:layout_width="60dp"   
    android:layout_height="80dp"
    android:layout_toRightOf="@+id/btn1"
    android:layout_alignTop="@+id/btn1"
    android:layout_marginLeft="10dp" 
    android:clickable="true"  
    android:tag="2"  
    android:background="@android:color/darker_gray" 
/>  
<ImageButton android:id="@+id/btn3"  
    android:layout_width="60dp"   
    android:layout_height="80dp"
    android:layout_toRightOf="@+id/btn2"
    android:layout_alignTop="@+id/btn2"
    android:layout_marginLeft="10dp"  
    android:clickable="true"  
    android:tag="3"  
    android:background="@android:color/darker_gray" 
/>  
</RelativeLayout>

Aligned with your left btn1.

If you want your btn2 at the center, use android:layout_centerInParent="true".

and let btn1 and btn3 align it.

LianYong Lee
  • 321
  • 3
  • 5
  • Thank you for your help, but I think this don't help me, because I want to display 9 buttons in the middle of the display, three on each row. In conclusion a big square that is composed from 9 little buttons displayed in the middle of the screen. – Andreea Oct 23 '12 at 20:07