简体   繁体   中英

Position Of Views in Android vs iOS?

I am new to Android development. I have been working in iOS since long. As in iOS when we want to put VIEW on xib on some exact position, we simply put it there, drag it up to that point.

For example say Two buttons at lower area in iOS, which look like below 在此输入图像描述

As, I simply want them in middle, I will put them their. as below

在此输入图像描述

Now same thing in Android environment, I go for following code,

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

    <TextView
        android:id="@+id/myAwesomeTextView"
        android:layout_width="wrap_content"
        android:layout_height="1dip"
        android:layout_centerInParent="true"
        android:text="Veer Suthar" />

    <TextView
        android:id="@+id/myAwesomeTextView1"
        android:layout_width="wrap_content"
        android:layout_height="100dip"
        android:layout_below="@id/myAwesomeTextView"
        android:layout_centerInParent="true" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/myAwesomeTextView1"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:onClick="buttonPressed"
            android:text="Button One" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:onClick="buttonPressed"
            android:text="Button Two" />
    </LinearLayout>

</RelativeLayout>

It shows Activity Screen, like below

在此输入图像描述

Now If I want to drag buttons, using GRAPHICAL LAYOUT , I can't move them as I want, and for spacing to put them into lower area, I need to put extra TextView .

Is there any better way to organise Android Activity GUI properly, like iOS?

Add this to your LinearLayout :

android:layout_alignParentBottom = "true"

Childs in a RelativeLayout can be "glued" to a particular position relative to the parent layout or to other elements in the same layout using the xml tags listed here: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

I'll give you a brief example, since Android graphical layout is not as smooth as XCode.

To accomplish what you need, centering the two buttons in the screen, you can use a XML code like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/layout_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true">

        <Button 
            android:id="@+id/button_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button One"/>

        <Button 
            android:id="@+id/button_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Two"/>
     </LinearLayout>

</RelativeLayout>

The trick is to use android:layout_centerInParent="true" for the only component that you want to be centered in the screen all other components can use that one for reference to be placed in the screen.

For example

<TextView
    android:id="@+id/myAwesomeTextView1"
    android:layout_width="wrap_content"
    android:layout_height="100dip"
    android:layout_above="@+id/layout_center"
    android:text="Veer Suthar"/>

This is one way for doing this, you can always find a better and more comprehensible way to do things. Hope this helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM