Button always displays on top in FrameLayout

2019-01-11 22:29发布

I have FrameLayout like this:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="changeColor"
        android:text="new button"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="some text"/>

</FrameLayout>

The problem is that the button is displayed on top while FrameLayout class overview tells us this: "Child views are drawn in a stack, with the most recently added child on top".

7条回答
甜甜的少女心
2楼-- · 2019-01-11 22:58

For API < 21 you cant use android:stateListAnimator="@null" or change the elevation. In my case I used 2 frame layouts embedded in a constraint layout. As the frame layouts can be stacked upon each other there is no need to change the elevation of the textview.

<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    >
    <Button
        android:id="@+id/my_daybutton"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/cal_button_background"
        android:textColor="@color/colorPrimaryDark"
        android:gravity="start|top"
        android:paddingTop="2dp"
        android:paddingStart="2dp"
        android:paddingEnd="2dp"
        />
</FrameLayout>

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bla"
        android:textSize="9sp"
        android:textColor="@android:color/holo_red_dark"
        android:layout_gravity="bottom|end"
        />
</FrameLayout>

查看更多
登录 后发表回答