【问题标题】:Empty view appears in unexpected place in Android空视图出现在Android中的意外位置
【发布时间】:2016-05-31 11:12:56
【问题描述】:

我有这个布局:

<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">

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:orientation="horizontal"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/mapbox_fill_white"/>



    </LinearLayout>

    <View
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="100dp">
    </View>


</RelativeLayout>

当我添加下面的视图时,它出现在线性布局的上方,但我希望它位于布局下方。为什么它会超出布局?我怎样才能解决这个问题?谢谢。

【问题讨论】:

    标签: android xml android-layout android-studio android-relativelayout


    【解决方案1】:

    使用属性 android:layout_below 将其显示在 LinearLayout 下方,

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    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" >
    
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerInParent="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:orientation="horizontal" >
    
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="asdf"
                android:background="@color/mapbox_fill_white" />
        </LinearLayout >
    
        <View
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_below="@+id/linearLayout"
            android:layout_weight="1" >
        </View >
    </RelativeLayout >
    

    android:layout_below -- 将此视图的上边缘定位在给定的锚视图 ID 下方。容纳此视图的上边距和下边距 锚点视图的边距。

    必须是对另一个资源的引用,格式为 "@[+][package:]type:name" 或表单中的主题属性 “?[包:][类型:]名称”。

    检查RelativeLayout LayoutParams

    【讨论】:

      【解决方案2】:

      您使用的是相对布局,要在您的线性布局下方显示此视图,您必须为视图分配规则 layout_below,为您的线性布局提供一个 id,然后在您的视图中放置

        android:layout_below="@id/your_linear_layout_id"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多