【问题标题】:Button always displays on top in FrameLayout按钮始终显示在 FrameLayout 的顶部
【发布时间】:2015-11-25 06:02:37
【问题描述】:

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

【问题讨论】:

标签: android android-framelayout


【解决方案1】:

This answer

Lollipop 及更高版本中的按钮具有默认高度 导致他们总是在上面画画。您可以通过覆盖来更改它 默认的 StateListAnimator。

尝试将其放入您的按钮 XML:

android:stateListAnimator="@null"

FrameLayout 现在应该覆盖 按钮。

【讨论】:

  • 你拯救了我的一天。这种东西应该更好地宣传。
  • 这个答案救了我。但我更愿意将 android 使用的系统按钮高度添加到框架布局中的其他视图中。我怎样才能获得 android 正在设置的按钮高度 dp 值?
  • 在我的测试中,这消除了非常好的默认按钮阴影。
  • 是的,这会删除使用高程的动画。你的按钮现在变成了一个简单的 ImageView。正确的解决方案是将其他项目的高度调整为高于您接受答案的按钮。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    将您的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>
    

    【讨论】:

      【解决方案4】:

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

      FrameLayout 被设计为在屏幕上屏蔽一个区域显示 单个项目。 一般情况下,应使用 FrameLayout 来容纳单个项目。 子视图,因为在一个子视图中组织子视图可能很困难 在没有孩子的情况下可扩展到不同屏幕尺寸的方式 相互重叠。但是,您可以将多个孩子添加到 FrameLayout 并通过以下方式控制它们在 FrameLayout 中的位置 使用 android:layout_gravity 为每个孩子分配重力 属性。

      最好将ButtonTextview 放在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="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>
      

      【讨论】:

        【解决方案5】:

        显然 android:stateListAnimator="@null" 仅适用于 API = 21 或更高版本, 所以对于那些以 API

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

        【讨论】:

          【解决方案6】:

          FrameLayout 应该用于保存single child view,因为在没有子级相互重叠的情况下,很难以可扩展到不同屏幕尺寸的方式组织子视图

          您应该在 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>
          

          【讨论】:

          • 为什么这么难?您可以按照他们在xml中获得的顺序吗?这就是文档解释它的方式。我只是知道它是如何工作的。为什么它与按钮和图像视图不同。它甚至不应该知道这一点。
          【解决方案7】:

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

          【讨论】:

            【解决方案8】:

            只需将高程 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"/>
            

            【讨论】:

              猜你喜欢
              • 2016-06-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-19
              • 2018-12-02
              • 1970-01-01
              • 1970-01-01
              • 2016-07-27
              相关资源
              最近更新 更多