相对布局中的z-index [英] z-index in Relative layout

查看:130
本文介绍了相对布局中的z-index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在RelativeLayout中的Button上方放置一个TextView.但它不起作用:TextView始终位于Button下方,即使我将其移到Button之前也是如此. 我也尝试过bringChildToFront()函数将textview移到前面,但是没有运气.

I need to place a TextView above the Button in RelativeLayout. But it is not working: TextView is always under the Button, even if I move it before the button. I also tried bringChildToFront() function to move textview on front, but no luck.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlBottom"
android:layout_width="fill_parent"
android:layout_height="match_parent">

<Button
    android:id="@+id/bVio"
    android:layout_width="wrap_content"
    android:layout_height="50dip"
    android:layout_alignParentLeft="true"
    android:text="VIO"></Button>

    <TextView
        android:id="@+id/bVio_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/bVio"
        android:layout_marginTop="6dip"
        android:layout_marginRight="6dip"
        android:text="00"
        android:textSize="15dip"
        android:textColor="#FF0000"/>
</RelativeLayout>

推荐答案

我偶然发现了这个问题,但是我在这里解决了.

I stumbled upon this question a little late, but here’s I how solved it.

当您使用具有海拔高度的项目(例如Button)时,在API级别为21(Lollipop/Android 5.0)及更高版本的Android上会发生此问题.

The problem occurs on Android with an API level 21 (Lollipop/Android 5.0) and above when you work with items which have an elevation, such as the Button.

在Lollipop之前的设备上订购布局时,Android将查看XML中各项的顺序. XML中的项目越靠下,Z-index中的项目就越靠上.因此,如果您将Button放在前面,并将TextField放在它下面,它将起作用.

When ordering the layout on pre Lollipop devices Android will look to the order of the items in the XML. The further down an item is in the XML the higher up it is in Z-index. So if you place the Button first and the TextField below it, it will work.

但是,当在具有高于API 21的Android的设备上确定z-index的顺序时,系统还将查看项目标高,并在标高值较低的项目上方渲染标高值较高的项目.因此,要使您的设计正常工作,必须确保TextField的高度高于Button的高度,这可以通过在项目上定义android:elevation来实现.下面的模拟XML适用于API级别21之前和之后的设备:

However, when deciding the order of the z-index on devices with Android above API 21 the system will also look at the items elevation and render items with a higher elevation value above items with a lower elevation value. So to get your design to work you must make sure that your TextField has a higher elevation than your Button which you can achieve by defining android:elevation on your items. The mock XML below works on devices pre and post API level 21:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlBottom"
    android:layout_width="fill_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/bVio"
        android:layout_width="wrap_content"
        android:layout_height="50dip"
        android:layout_alignParentLeft="true"
        android:text="VIO"
        android:elevation="0dp"
        />

    <TextView
        android:id="@+id/bVio_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/bVio"
        android:layout_marginTop="6dip"
        android:layout_marginRight="6dip"
        android:text="00"
        android:textSize="15dip"
        android:textColor="#FF0000"
        android:elevation="10dp"
        />

</RelativeLayout>

这篇关于相对布局中的z-index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆