(上面)按钮上的Textview在Android 5(API 21)中不起作用

Evg*_*kov 12 android position

它看起来像5 Android(API 21)中的错误.我需要一个textview on button,textview应该放在按钮上方.它在Android 4.1(API 16)上运行正常,在5 Android(API 21)上运行不正确.有截图和代码:

Android 4.1 - 这是正确的,按钮上方的红色文本视图

Android 4.1

Android 5 - 它是不正确的,按钮下的红色textview!

Android 5

码:

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



<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:background="#00FF00">

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

    <TextView
        android:id="@+id/bVio_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00"
        android:textSize="12dip"
        android:textColor="#FFFFFF"
        android:background="@drawable/rounded_textbox"/>

    </FrameLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

rounded_textbox - 它只是形状...如果删除背景,所有看起来相同,textview在5 android下的按钮下.

请指教!

Evg*_*kov 26

是.这是Android L(API 21)中的重要内容.有新东西 - Elevation,它类似于HTML中的z-index.所以要修复这个bug你需要使用android:elevation ="100dp"或者android:translationZ ="100dip"来查看应该在顶部.所以正确的代码是:

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



<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:background="#00FF00">

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

    <TextView
        android:id="@+id/bVio_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00"
        android:textSize="12dip"
        android:textColor="#FFFFFF"
        android:background="@drawable/rounded_textbox"
        android:elevation="100dp"/>

    </FrameLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

  • android:translationZ ="100dip"不起作用,提升值可以大于"1dp"(例如:android:elevation ="2dp"). (2认同)