android - 在RelativeLayout布局中layout_above不起作用
PHP中文网
PHP中文网 2017-04-17 15:57:08
[Android讨论组]

布局文件代码如下:
layout_above没有起作用,listview把textview覆盖了

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

    <ListView
        android:id="@+id/listView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/listView2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="20dp"
        android:text="TextView" />

</RelativeLayout>

但是在listview中添加属性android:layout_below="@+id/textView1"就没有问题

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(3)
天蓬老师

因为wrap_contentListView是不起效的,还是会撑满整个屏幕高度,先写的ListView,屏幕已经无空间了,再加TextView自然是看不见的(而不是覆盖)。还有,严谨的相对id写法应该是android:layout_below="@id/textView1"

PHP中文网

这样既可:

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

    <ListView
        android:layout_below="@id/textView1"
        android:id="@+id/listView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="20dp"
        android:text="TextView" />

</RelativeLayout>
PHPz

视图树是从外向内,从上向下的顺序去绘制view,前一个的测量和布局方法,会影响到后者,所以依赖关系尽量放在前面一个视图去约束。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号