按钮始终显示在 FrameLayout 的顶部

Button always displays on top in FrameLayout

我有这样的 FrameLayout:

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

问题是按钮显示在顶部,而 FrameLayout class 概述告诉我们:"Child views are drawn in a stack, with the most recently added child on top"。

正如官方 android 文档指出的那样:

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

最好将 ButtonTextview 放在 FrameLayout 内的 RelativeLayout 中,例如:

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

FrameLayout 应该用于保存 single child view,因为如果没有子 [=,很难以可缩放到不同屏幕尺寸的方式组织子视图17=]相互重叠

您应该在 FrameLayout 中使用 LinearLayoutRelativeLayout 。 像这样

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

Buttons in Lollipop and higher have a default elevation to them which causes them to always draw on top. You can change this by overriding the default StateListAnimator.

Try putting this into your button XML:

android:stateListAnimator="@null"

The FrameLayout should now cover the button.

在Android 5.0 (API 21) 及更高版本中,您必须将 android:elevation 添加到视图中。

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

把你的Button放在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>

对于 API < 21,您不能使用 android:stateListAnimator="@null" 或更改高度。在我的例子中,我使用了嵌入在约束布局中的 2 个框架布局。 由于框架布局可以相互堆叠,因此无需更改文本视图的高度。

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

显然 android:stateListAnimator="@null" 仅适用于 API = 21 或更高, 所以对于那些目标 API<21 使用这个的人来说,它对我有用 :D

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <FrameLayout
    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"/>
    </FrameLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="some text"/>
</FrameLayout>

只需将 elevation FrameLayout 或您要加载的任何父级放在那里

android:elevation="@dimen/dimen_20"

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/dimen_0"
    android:id="@+id/ready_to_scan"
    app:layout_constraintTop_toTopOf="parent"
    android:elevation="@dimen/dimen_20"
    app:layout_constraintBottom_toBottomOf="parent"/>