gpt4 book ai didi

java - Android 与 iOS 中 View 的位置?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:15 24 4
gpt4 key购买 nike

我是 Android 开发新手。我一直在 iOS 工作。就像在 iOS 中一样,当我们想将 xib 上的 VIEW 放在某个确切位置时,我们只需将它放在那里,然后将其拖动到该点即可。

例如在 iOS 的下部区域有两个按钮,如下所示 enter image description here

因为,我只是想让它们在中间,我会把它们放在它们的中间。如下

enter image description here

现在在 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>

它显示 Activity 屏幕,如下所示

enter image description here

现在如果我想拖动按钮,使用GRAPHICAL LAYOUT,我不能随心所欲地移动它们,为了将它们放到较低区域的间距,我需要放置额外的 TextView .

有没有更好的方法来正确组织 Android Activity GUI,就像 iOS 一样?

最佳答案

我会给你一个简短的例子,因为 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"/>

这是一种方法,您总能找到更好、更易于理解的方法来做事。希望这对您有所帮助。

关于java - Android 与 iOS 中 View 的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21761914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com