简体   繁体   English

Android:相对布局

[英]Android: Relative Layout

Been using android for about 3 months and always hardcode the width and height of elements in a layout, I know this is considered bad to do so I want to get a better understand of how to use relative layouts to position elements. 一直使用android大约3个月,并且始终对布局中元素的宽度和高度进行硬编码,我知道这样做被认为是不好的,所以我想更好地了解如何使用相对布局来定位元素。 I have looked at the documentation of relative layout but from my understanding, I cannot get it to work the way I would like. 我看过相对布局的文档,但是据我了解,我无法使其按自己的方式工作。 I come asking if anyone can provide help (not just suggesting documentation I have read it) with how I need to use relative layout. 我来问是否有人可以提供有关如何使用相对布局的帮助(不仅仅是建议我阅读过的文档)。 This is so I can have something basic to refer to in future work to help with my positioning. 这样一来,我便可以在以后的工作中参考一些基本信息,以帮助我进行定位。

The following is 3 elements contained on a relative layout, 2 expandable lists and 1 listview: 以下是相对布局中包含的3个元素,2个可扩展列表和1个listview: 在此处输入图片说明

The listviews are all over the place in this example I would like to know how to position them something like this: 在这个例子中,列表视图无处不在,我想知道如何将它们定位成这样: 在此处输入图片说明

this is the current xml, what would I need to add and change to gain this type of layout? 这是当前的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>

Thank you for any help. 感谢您的任何帮助。

With RelativeLayout, your main tools at hand are: 使用RelativeLayout,您手边的主要工具是:

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

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

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

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

ETC. 等等。 ETC. 等等。 Check this: https://www.tutorialspoint.com/android/android_relative_layout.htm 检查以下内容: https : //www.tutorialspoint.com/android/android_relative_layout.htm

So when using RelativeLayout, you specify the position of all the views relative to each other using the syntax given above. 因此,在使用RelativeLayout时,您可以使用上述语法指定所有视图之间的相对位置。 That's basically all to it. 基本上就这些了。

Just don't use drag and drop and type the code by yourself. 只是不要使用拖放操作并自行键入代码。 Use Ctrl + space and the list with all the attributes will show. 使用Ctrl +空格,将显示具有所有属性的列表。 Now you just need to know what you what to do. 现在,您只需要知道该做什么。 In your example you need one view "android:layout_toRightOf" other so just type right and android studio will automatically show you the attribute that you want. 在您的示例中,您需要一个视图“ android:layout_toRightOf”,因此只需键入right即可,android studio会自动向您显示所需的属性。 In case of placing a view below another view you can use "android:layout_below". 如果将视图放置在另一个视图下,则可以使用“ android:layout_below”。 You can read all the attributes 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>

Try this way... Change the margins and weights as you need. 尝试这种方式...根据需要更改边距和权重。 You can customize the ratio of each listview by changing the weights. 您可以通过更改权重来自定义每个列表视图的比率。

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ExpandableListView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="@dimen/activity_vertical_margin"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_dark" />

        <ExpandableListView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="@dimen/activity_vertical_margin"
            android:layout_weight="1"
            android:background="@android:color/holo_green_dark" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">


        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="@dimen/activity_vertical_margin"
            android:background="@android:color/holo_red_light" />
    </LinearLayout>


</LinearLayout>

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

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