Android 按钮始终显示在FrameLayout的顶部

Android 按钮始终显示在FrameLayout的顶部,android,android-framelayout,Android,Android 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

我有这样的框架布局:

<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类概述告诉我们:“子视图绘制在堆栈中,最新添加的子视图位于顶部”。

正如官方android文档所指出的:

FrameLayout设计用于在屏幕上屏蔽要显示的区域 单个项目。通常,框架布局应用于容纳单个项目 子视图,因为在一个视图中组织子视图可能很困难 这种方式可以扩展到不同的屏幕大小,而不需要孩子 相互重叠。但是,您可以将多个子项添加到 FrameLayout并通过以下方式控制其在FrameLayout中的位置 使用android:layout\u gravity为每个孩子分配重力 属性

最好将
按钮
文本视图
放在
相对视图
框架布局内,如:

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

棒棒糖和更高版本中的按钮有一个默认的标高,该标高 使它们始终在顶部绘制。您可以通过重写 默认的StateListAnimator

尝试将其放入按钮XML中:

android:stateListAnimator=“@null”

框架布局现在应该覆盖 按钮

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

将您的
按钮
放入
框架布局

<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”或更改标高。在我的例子中,我使用了嵌入在约束布局中的两个框架布局。 由于框架布局可以相互堆叠,因此无需更改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>


显然android:stateListAnimator=“@null”仅适用于API=21或更高版本,
所以对于那些以API为目标的人,你可以使用线性布局来解释为什么这很难?你可以把它们在xml中的顺序放进去吗?文档就是这样解释的。我只知道它是怎么工作的。为什么它与按钮和图像视图不同。它根本不应该知道,你救了我一天。这类事情最好做广告。这个答案救了我。但我更愿意将android使用的系统按钮高程添加到framelayout中的其他视图中。如何获得android设置的按钮高度dp valu?在我的测试中,这将删除非常好的默认按钮阴影。是的,这将删除使用高度的动画。您的按钮现在成为一个简单的图像视图。正确的解决方法是将其他项目的标高调整为高于您的按钮,该按钮是可接受的答案。