Bouton affiche toujours sur le dessus dans FrameLayout

J'ai FrameLayout comme ceci:

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

Le problème est que le bouton est affiché en haut alors que FrameLayout class overview nous dit ceci: "les vues enfants sont dessinées dans une pile, avec l'enfant le plus récemment ajouté en haut".

24
demandé sur Nokuap 2015-08-31 11:47:30

6 réponses

Cette réponse

Boutons dans Lollipop et supérieur ont une élévation par défaut à eux qui les amène à toujours dessiner sur le dessus. Vous pouvez changer cela en remplaçant le StateListAnimator par défaut.

Essayez de mettre ceci dans votre bouton XML:

android:stateListAnimator="@null"

Le FrameLayout devrait maintenant couvrir bouton.

42
répondu Rahul Tiwari 2017-05-23 12:18:17

Dans Android 5.0 (API 21) et au-dessus, vous devez ajouter android:elevation dans la vue.

<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"
        android:elevation="3dp"/>
12
répondu Kyle Vo 2016-03-02 08:42:21

FrameLayout doit être utilisé pour tenir une single child view, car il peut être difficile d'organiser les vues enfant de manière évolutive à différentes tailles d'écran sans les enfants qui se chevauchent les uns les autres.

Vous devez utiliser LinearLayout ou RelativeLayout dans FrameLayout . Comme cette façon

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

   <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_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"/>
   </RelativeLayout>

</FrameLayout>
1
répondu IntelliJ Amiya 2015-08-31 08:58:22

Comme le souligne la documantation officielle android:

FrameLayout est conçu pour bloquer une zone de l'écran à afficher un seul élément. Généralement, FrameLayout doit être utilisé pour vue enfant, car il peut être difficile d'organiser des vues enfant dans un de manière évolutive à différentes tailles d'écran sans les enfants imbriqués les uns dans les autres. Toutefois, vous pouvez ajouter plusieurs enfants à un FrameLayout et contrôler leur position dans le FrameLayout par attribuer la gravité à chaque enfant, en utilisant android: layout_gravity attribut.

, C'est mieux si vous mettez votre Button et Textview dans a RelativeLayout dans FrameLayout comme:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="some text"/>
        <Button
            android:id="@+id/button1"
            android:layout_below="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="changeColor"
            android:text="new button"/>
        <RelativeLayout>
    </FrameLayout>
1
répondu zkminusck 2015-08-31 09:16:49

Mettez dans votre Button à l'intérieur FrameLayout

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

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="some text" />
</RelativeLayout>
0
répondu Anril 2017-12-25 14:47:40

Pour API

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

0
répondu Thoms 2018-06-15 12:47:30