垂直布局不起作用

vertical layout not working

我刚开始使用 Android,我正在观看视频指南。 在视频中,每当他添加一个 TextView 时,它都会以垂直方式添加... 但对我来说,它会将每个 TextView 一个加在另一个之上。没有任何顺序。 这是我的初始代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.thebasics.MainActivity" >

  <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="wassup" />
  <TextView
            android:id="@+id/textView3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="41dp"
            android:text="@string/hello_world" />

    </RelativeLayout>

我添加了 android:orientation="vertical" 但没有用。我没有收到任何错误……它什么都不做。我仍然知道它将对象一个放在另一个上。

我该怎么做才能解决这个问题?我想要两个 TextViews 的垂直顺序。 我在别处找不到答案,视频指南默认得到它,所以他没有解释如果它不起作用该怎么做。

您需要使用 LinearLayout 作为根元素,而不是 RelativeLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.thebasics.MainActivity" >

android:orientation="vertical" 用于 LinearLayouts,而您使用的是 RelativeLayout

如果你想让你的文本视图一个在另一个上面,只需将你的 Relatives 替换为 Linear 并将方向设置为垂直:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.thebasics.MainActivity" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="wassup" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="41dp"
    android:text="@string/hello_world" />

</LinearLayout>

it adds each textView one on top of the other.. no any kind of order

居然有订单。 RelativeLayout 默认将 Views 放在左上角,因此如果您愿意,它会使用默认的 "order"。您需要使用 RelativeLayout 的不同布局属性来定位它们。

i added android:orientation="vertical"

RelativeLayout 没有 orientation 属性 所以这不会有任何效果。

what shall i do to solve this?

您可以使用 android:layout_below="@id/idOfAboveTV",但这可能很麻烦,除非您出于其他原因 need/want RelativeLayout

您可以将其更改为 LinearLayout,因为 LL 的默认 orientationhorizontal,只需添加 android:orientation="vertical",您的 Views 将是以垂直方式布置。

注意: 如果您使用 LinearLayout,那么您需要删除一些 View 特定的 RelativeLayout 参数,例如作为 android:layout_alignParentTop="true"

有用文档的链接

RelativeLayout

Relative Layout Layout Params

LinearLayout

LinearLayout Layout Params