相对布局中的z-index

问题描述 投票:15回答:4

我需要在RelativeLayout中的Button上方放置一个TextView。但它不起作用:TextView总是在Button下,即使我在按钮之前移动它。我也试过bringChildToFront()功能在前面移动textview,但没有运气。

<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>
android z-index android-relativelayout
4个回答
38
投票

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

当您使用具有高程的项目(例如Button)时,问题出现在具有API级别21(Lollipop / Android 5.0)及更高级别的Android上。

在Lollipop设备上订购布局时,Android会查看XML中项目的顺序。项目在XML中越往下,它在Z-index中越高。因此,如果您首先放置Button并将TextField放在它下面,它将起作用。

但是,在使用Android 21以上的设备上决定z-index的顺序时,系统还会查看项目高程,并在具有较低高程值的项目之上呈现具有较高高程值的项目。因此,要使您的设计正常工作,您必须确保TextField的高度高于Button,您可以通过在项目上定义android:elevation来实现。下面的模拟XML适用于API级别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>

0
投票

将RelativeLayout作为您的按钮,通过更改其背景并将TextView设置为centerInParent =“True”

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

    <TextView
        android:id="@+id/bVio_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="I am khan "
        android:textColor="#FF0000"
        android:textSize="35dip" />

</RelativeLayout>

(RelativeLayout)findViewById(R.id.rlBottom).setOnClick(new OnClick... //like a button

0
投票

添加此代码,它将解决您的问题。

android:stateListAnimator="@null" 

参考this answer

Lollipop及更高版本中的按钮具有默认高度,这使得它们始终在顶部绘制。您可以通过覆盖默认的StateListAnimator来更改此设置。

尝试将其放入按钮XML:

android:stateListAnimator="@null"

FrameLayout现在应该覆盖按钮。


-2
投票

首先放置TextView,而不是将layout_below应用于按钮:

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

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

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

</RelativeLayout>
© www.soinside.com 2019 - 2024. All rights reserved.