首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RelativeLayout :视图间相对变化的不同结果

RelativeLayout :视图间相对变化的不同结果
EN

Stack Overflow用户
提问于 2013-11-11 07:17:20
回答 3查看 86关注 0票数 0

我有这样的布局:首先,是一个旋转器,之后是一个列表视图,然后是一个线性布局,包括EditText和Button,屏幕底部是一个按钮。

下面是我的第一个布局文件:

代码语言:javascript
运行
复制
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical">

    <Spinner
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textColor="#0e0e0e"
        android:layout_above="@+id/list_view"
        android:id="@+id/spinner"/>

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/list_view"
        android:layout_alignParentLeft="true"
        android:layout_above="@+id/linear_layout" />


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/linear_layout"
        android:layout_above="@+id/search_btn"
        android:weightSum="3"
        android:layout_alignParentLeft="true">

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/insert_chord_edit_text"
            android:layout_weight="2"
            android:text=""/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/add_chord_button"
            android:layout_weight="1"
            android:text="Add"/>

    </LinearLayout>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/search_btn"
        android:text="Search"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="32dp" />

</RelativeLayout>

一切都很好,除了我看不到Spinner。(我已经在真正的设备上进行了测试)。在那之后,我改变上面的布局。而不是说:spinner is above listviewlistview is above of linearlayout。我改为:listview is below of spinnerlistview is above of linearlayout。我有如下更改:

代码语言:javascript
运行
复制
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical">

    <Spinner
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textColor="#0e0e0e"
        android:id="@+id/spinner"/>

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/list_view"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/spinner"       // ADD THIS LINE
        android:layout_above="@+id/linear_layout" />


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/linear_layout"
        android:layout_above="@+id/search_btn"
        android:weightSum="3"
        android:layout_alignParentLeft="true">

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/insert_chord_edit_text"
            android:layout_weight="2"
            android:text=""/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/add_chord_button"
            android:layout_weight="1"
            android:text="Add"/>

    </LinearLayout>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/search_btn"
        android:text="Search"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="32dp" />

</RelativeLayout>

它能按我的意愿工作,但我无法解释原因。请告诉我。

谢谢:)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-11-11 07:25:27

在第一种情况下,您的listview被提到为高度填充父级,它有layout_above属性,但没有layout_below属性。因此,listview占据了linear_layout元素之上的所有高度。这就是为什么看不到spinner.as的原因--旋转器的可见性被listview覆盖。

票数 1
EN

Stack Overflow用户

发布于 2013-11-11 09:28:37

代码语言:javascript
运行
复制
// try this
<?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="wrap_content"
    android:padding="5dp"
    android:orientation="vertical" >

    <Spinner
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textColor="#0e0e0e"
        android:id="@+id/spinner"/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/list_view"/>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="5dp"
        android:id="@+id/linear_layout">

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/insert_chord_edit_text"
            android:layout_weight="2"
            android:text=""/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/add_chord_button"
            android:layout_weight="1"
            android:text="Add"/>

    </LinearLayout>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/search_btn"
        android:text="Search"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"/>
</LinearLayout>
票数 1
EN

Stack Overflow用户

发布于 2013-11-11 08:16:19

为您的旋转器设置layout_weight="1"可以解决您的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19900827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档