Android layout_above的使用注意

要写一个菜单,菜单结构很简单,一个标题和一个列表,但是设计只给出了列表的坐标,以及标题在列表上方8dp的位置,没多想,直接用layout_above将标题放到listview的上方,但是发现标题不见了,

代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="414dp"
    android:layout_height="382dp"
    android:background="#992D2926">

    <ListView
        android:id="@+id/list"
        android:layout_width="352dp"
        android:layout_height="338dp"
        android:layout_marginLeft="54dp"
        android:layout_marginTop="36dp"
        android:background="#992D2926" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/list"
        android:layout_marginLeft="8dp"
        android:layout_marginBottom="8dp"
        android:text="Title"
        android:textColor="@color/colorWhite"
        android:textSize="14sp" />

</RelativeLayout>

预览图:

可以看到左上角有一个向下的箭头以及数字8,至此就明白了,title被挤到屏幕外了;

原本预期结果是title位于list上方8dp处,但实际结果title位于屏幕的上方8dp处。

修改一下代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="414dp"
    android:layout_height="382dp"
    android:background="#992D2926">

    <ListView
        android:id="@+id/list"
        android:layout_width="352dp"
        android:layout_height="338dp"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="54dp"
        android:layout_marginBottom="8dp"
        android:background="#992D2926" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/list"
        android:layout_marginLeft="8dp"
        android:layout_marginBottom="8dp"
        android:text="Title"
        android:textColor="@color/colorWhite"
        android:textSize="14sp" />

</RelativeLayout>

预览图:

OK,Title出来了;修改部分为将 layout_marginTop 修改为 layout_marginBottom。

总结:

控件的排列方向需要一致,这里出问题就是因为listview是从上往下排的(layout_marginTop),而Title是从下往上排(layout_above),导致出问题

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值