【问题标题】:How to add a TextView above a FloatingActionButton in Android如何在 Android 中的 FloatingActionButton 上方添加 TextView
【发布时间】:2015-11-27 08:34:55
【问题描述】:

我想在 FloatingActionButton 上方添加一个 TextView,我使用 FrameLayout 作为 TextView 和 FloatingActionButton 的父布局,这是我的示例代码:

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

    <android.support.design.widget.FloatingActionButton
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="#00fff0"
        app:borderWidth="0dp"
        android:elevation="0dp"
        app:fabSize="normal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="above"/>

</FrameLayout>

但是没用,TextView在FloatingActionButton下面,像这样

我希望它显示如下:

我很穷,谁能帮帮我?

【问题讨论】:

    标签: android floating-action-button


    【解决方案1】:

    你只需要在文本视图中添加一个海拔属性

     <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <android.support.design.widget.FloatingActionButton
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:backgroundTint="#00fff0"
            app:borderWidth="0dp"
            android:elevation="0dp"
            app:fabSize="normal" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:text="above"
            android:elevation="7dp"/>
    
    </FrameLayout>
    

    【讨论】:

    • 欢迎......快乐编码:)
    • 使用app:elevation支持低于API 21的版本。
    • 您将 FAB 标高设置为零,如果 FAB 没有标高,这是否违背了拥有 FAB 的目的。我的观点是,如果您不将 FAB 的 app:elevation 设置为零,这将不起作用。尝试将FAB设置为海拔7,textview设置为8,textview仍然不会出现在顶部。
    【解决方案2】:

    这是一个 z 顺序问题。在您的 FrameLayout 中,将 &lt;TextView 移动到 &lt;FloatingActionButton 上方

    <FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="above"/>
    
      <android.support.design.widget.FloatingActionButton
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="#00fff0"
        app:borderWidth="0dp"
        android:elevation="0dp"
        app:fabSize="normal" />
    </FrameLayout>
    

    应该这样做

    【讨论】:

    • 我试过了,没用
    • 漂亮!像 API>21 的魅力一样工作。不得不提高电视的高度。你可以做任何一件事。 为 TextView 提供高度或从 FAB 中移除高度
    【解决方案3】:

    要支持低端设备,请同时使用 android:elevationapp:elevation

    将 Fab 的两个值都设置为 0

     <FrameLayout
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
    
    <android.support.design.widget.FloatingActionButton
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="#00fff0"
        app:borderWidth="0dp"
        android:elevation="0dp"
        app:elevation="0dp"
        app:fabSize="normal" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="above"/>
    
        </FrameLayout>
    

    【讨论】:

      【解决方案4】:

      这是您的解决方案:

      <FrameLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
      
      <RelativeLayout 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" >
      
      
      <android.support.design.widget.FloatingActionButton
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:backgroundTint="#00fff0"
          app:borderWidth="0dp"
          android:elevation="0dp"
          app:fabSize="normal" />
      
      <TextView
          android:layout_toRightOf="@+id/fab"
          android:layout_marginLeft="-10dp"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="#000000"
          android:text="above"/>
      </RelativeLayout>
      
      </FrameLayout>
      

      【讨论】:

        【解决方案5】:

        相应地为两个视图设置标高。此外,将 TextView XML 代码移到 FAB XML 代码上方对您来说更有意义。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-13
          • 1970-01-01
          相关资源
          最近更新 更多