Android:相对布局

Android: Relative Layout

使用 android 大约 3 个月,并且总是对布局中元素的宽度和高度进行硬编码,我知道这样做很糟糕,所以我想更好地了解如何使用 relative用于定位元素的布局。我查看了相对布局的文档,但根据我的理解,我无法让它按照我想要的方式工作。我来问是否有人可以就我需要如何使用相对布局提供帮助(不仅仅是建议我已经阅读过的文档)。这样我在以后的工作中可以有一些基本的参考,帮助我定位。

以下是相对布局中包含的 3 个元素、2 个可扩展列表和 1 个列表视图:

列表视图在这个例子中无处不在我想知道如何像这样定位它们:

这是当前的 xml,我需要添加和更改什么才能获得这种类型的布局?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_food_bible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#606E7F"
tools:context="com.example.mr_br.project.food_bible">

<ExpandableListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/exlistVtype"
    android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>
<ExpandableListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/exlistVpref"
    android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" android:layout_marginLeft="114dp"
    android:layout_marginStart="114dp"/>
 <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="244dp"
        android:id="@+id/listVresult" android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/>
</RelativeLayout>

感谢您的帮助。

使用 RelativeLayout,您手头的主要工具是:

1)android:layout_above="@+id/theIdOfWhatYouWantBelowCurrentView"

2)android:layout_alignBottom = "@+id/IDofWhatYouWantTouchingBottomOfThisView

3)android:layout_alignLeft = "@+id/IDofWhatYouWantTouchingLeftOfThisView"

4)android:layout_below = "@+id/theIdOfWhatYouWantAboveCurrentView"

等等。等等。检查这个:https://www.tutorialspoint.com/android/android_relative_layout.htm

因此,当使用 RelativeLayout 时,您可以使用上面给出的语法指定所有视图相对于彼此的位置。基本上就这些了。

只是不要使用拖放并自己键入代码。使用 Ctrl + space 将显示包含所有属性的列表。现在你只需要知道你要做什么。在您的示例中,您需要一个视图 "android:layout_toRightOf" 其他视图,因此只需输入正确的内容,android studio 将自动向您显示您想要的属性。如果将一个视图放在另一个视图下方,您可以使用 "android:layout_below"。 可以读取所有属性here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_food_bible"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#606E7F"
    tools:context="com.example.mr_br.project.food_bible">

<ExpandableListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/exlistVtype"
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>
<ExpandableListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/exlistVpref"
    android:layout_toRightOf="@id/exlistVtype"
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" android:layout_marginLeft="114dp"
    android:layout_marginStart="114dp"/>
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="244dp"
    android:layout_below="@id/exlistVtype"
    android:id="@+id/listVresult" android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true"/>
</RelativeLayout>