[英]Position Of Views in Android vs iOS?
我是Android开发的新手。 我一直在iOS工作。 和iOS一样,当我们想在一个确切的位置放置xib上的VIEW时,我们只需将它放在那里,将它拖到那一点。
例如,在iOS的较低区域说两个按钮,如下所示
因为,我只是希望他们在中间,我会把他们。 如下
在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>
它显示活动屏幕,如下所示
现在如果我想拖动按钮,使用GRAPHICAL LAYOUT ,我无法按照我的意愿移动它们,并且为了将它们放入较低的区域,我需要添加额外的TextView 。
有没有更好的方法来正确组织Android Activity GUI,比如iOS?
将其添加到LinearLayout
:
android:layout_alignParentBottom = "true"
RelativeLayout
可以使用此处列出的xml标记“粘合”到相对于父布局的特定位置或相同布局中的其他元素: http : //developer.android.com/reference/android/widget/RelativeLayout .LayoutParams.html
我将举一个简短的例子,因为Android图形布局不如XCode那么流畅。
要完成您的需要,将两个按钮置于屏幕中心,您可以使用如下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>
诀窍是使用android:layout_centerInParent="true"
作为您希望在屏幕中居中的唯一组件,所有其他组件可以使用该组件作为参考放置在屏幕中。
例如
<TextView
android:id="@+id/myAwesomeTextView1"
android:layout_width="wrap_content"
android:layout_height="100dip"
android:layout_above="@+id/layout_center"
android:text="Veer Suthar"/>
这是实现这一目标的一种方式,您总能找到一种更好,更易于理解的方法。 希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.