简体   繁体   English

视图在Android vs iOS中的位置?

[英]Position Of Views in Android vs iOS?

I am new to Android development. 我是Android开发的新手。 I have been working in iOS since long. 我一直在iOS工作。 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. 和iOS一样,当我们想在一个确切的位置放置xib上的VIEW时,我们只需将它放在那里,将它拖到那一点。

For example say Two buttons at lower area in iOS, which look like below 例如,在iOS的较低区域说两个按钮,如下所示 在此输入图像描述

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, 在Android环境中同样的事情,我去寻找代码,

<?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 . 现在如果我想拖动按钮,使用GRAPHICAL LAYOUT ,我无法按照我的意愿移动它们,并且为了将它们放入较低的区域,我需要添加额外的TextView

Is there any better way to organise Android Activity GUI properly, like iOS? 有没有更好的方法来正确组织Android Activity GUI,比如iOS?

Add this to your LinearLayout : 将其添加到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 RelativeLayout可以使用此处列出的xml标记“粘合”到相对于父布局的特定位置或相同布局中的其他元素: 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. 我将举一个简短的例子,因为Android图形布局不如XCode那么流畅。

To accomplish what you need, centering the two buttons in the screen, you can use a XML code like this: 要完成您的需要,将两个按钮置于屏幕中心,您可以使用如下XML代码:

<?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. 诀窍是使用android:layout_centerInParent="true"作为您希望在屏幕中居中的唯一组件,所有其他组件可以使用该组件作为参考放置在屏幕中。

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. 希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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