Android活动全部显示在同一行

时间:2015-01-07 08:32:43

标签: android xml android-studio

我在Android Studio中遇到了xml代码问题。

由于某种原因,这个xml代码:

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:id="@+id/main_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/textview"/>
    <!-- Set OnClickListener to trigger results when pressed -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <!-- Displays keyboard when touched -->
    <EditText
        android:id="@+id/main_edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:hint="@string/hint" />
    <!-- This nested layout contains views of its own -->
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <!-- Set OnClickListener to trigger results when pressed -->
        <Button
            android:id="@+id/main_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:text="@string/button" />
        <!-- Shows an image from your drawable resources -->
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:src="@drawable/ic_launcher" />
        <!-- Closing tag for the horizontal nested layout -->
    </LinearLayout>
    <!-- Displays keyboard when touched -->

</LinearLayout>

当我运行我的应用程序时,所有人都出现在同一行。

如何让它像html中的br标签一样进入下一行。

感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

你应该添加 android:orientation="vertical" 到第一个(外部)LinearLayout

答案 1 :(得分:1)

对于LinearLayout:

  

默认方向是水平的。

只需将主布局中的方向设置为:

android:orientation="vertical"

Documentation

答案 2 :(得分:1)

<强>的LinearLayout

  

将其子项排列在单个列或单个列中的布局   行。可以通过调用setOrientation()来设置行的方向。   您还可以指定gravity,它指定所有的对齐方式   通过调用setGravity()或指定特定的子元素   孩子们通过设置来填充布局中的任何剩余空间   LinearLayout.LayoutParams的权重成员。 默认值   方向是水平的。

这就是为什么你把所有东西放在同一条线上的原因。