Textview on(above) Button, doesn't work in Android 5(API 21)

荒凉一梦 提交于 2019-12-04 18:36:21

问题


It looks like a bug in 5 Android(API 21). I need a textview on button, textview should placed above the button. It works correct on Android 4.1(API 16) and incorrect on 5 Android(API 21). There are Screenshots and code:

Android 4.1 - It is correct, red textview above the button

Android 5 - it is incorrect, red textview under the button!

Code:

<?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>

rounded_textbox - it is just shape... if remove background, all looks same, textview under button in 5 android.

Please, advice!


回答1:


Yes. It is big chnage in Android L(API 21). There is new thing - Elevation, it is something like z-index in HTML. So to fix this bug you need use android:elevation="100dp" OR android:translationZ="100dip" for view that should be on top. So correct code is:

<?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>


来源:https://stackoverflow.com/questions/27134400/textview-onabove-button-doesnt-work-in-android-5api-21

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!