【问题标题】:Floating Action Button bad rendering浮动操作按钮渲染错误
【发布时间】:2017-01-10 11:43:15
【问题描述】:

我的应用程序中的浮动操作按钮出现了一些问题。在 Android Studio 模拟器中,无论是在应用程序模拟器中的 xml 文件的“设计”视图中,它看起来都不错,但在真实设备中,当我运行应用程序时,它不是圆形,而是椭圆形. 此外,此错误仅在某些执行时发生...昨天 FAB 在我的设备上也是循环的,但今天不是(我没有修改 xml 部分,在 Java 方面我不处理它的方面)...

这是我定义按钮的 .xml 文件:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coor"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="1">

    <RelativeLayout
        android:id="@+id/relative"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:layout_weight="0.88">

   <!-- Other elements-->

   <!-- Parent element-->   

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/etEntrate"
            android:textSize="@dimen/subtitles"
            android:padding="@dimen/activity_horizontal_margin"
            android:layout_alignRight="@+id/data"
            android:layout_alignEnd="@+id/data"
            android:layout_below="@+id/etSpese"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginTop="@dimen/activity_horizontal_margin"
              android:layout_marginBottom="@dimen/activity_horizontal_margin"/>

  <!--FAB-->
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            app:srcCompat="@drawable/plus"
            android:layout_alignTop="@+id/etEntrate"
            android:layout_marginTop="59dp"
            android:layout_alignLeft="@+id/etEntrate"
            android:layout_alignStart="@+id/etEntrate"
            android:layout_alignRight="@+id/etEntrate"
            android:layout_alignEnd="@+id/etEntrate" />

   <!--other elements-->
    </RelativeLayout>
</LinearLayout>

这里有两张图片说明我如何在设备上看到 FAB 以及它在模拟器上的外观:

我设备上的 FAB:

Android Studio设计视图上的FAB

有人对此有所了解吗?谢谢!

【问题讨论】:

  • 首先使用android:src 设置FAB 可绘制然后你能告诉我为什么你将它与etEntrate 元素的左右对齐吗?画出你想要归档的草稿。咻!
  • 您使用的是哪个版本的设计支持库?
  • 尝试更改页边距。
  • 感恩@fillobotto!我读了好几遍,但我从来没有注意到这个愚蠢的错误......现在它是循环的,当然!如果我将 'android:srcCompat' 更改为 'android:src' 我会得到一个“找不到包中属性 src 的资源标识符...”,所以我使用的是 'srcCompat'。如果您回答问题,我可以接受并关闭它

标签: android android-layout floating-action-button


【解决方案1】:

当然,FAB 的宽度和高度设置为wrap_content,但您试图左右对齐,这就是它伸展的原因。 只需删除其中任何一个即可。

【讨论】:

    【解决方案2】:

    当您在水平方向使用重力时,您必须使用 layout_width=0dp 并且在垂直方向的情况下,layout_height=0dp 用于布局内的每个子项。

    只需从相对布局中删除浮动操作按钮,因为您已经在使用坐标布局。

    <android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coor"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
        <RelativeLayout
            android:id="@+id/relative"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="@dimen/activity_horizontal_margin"
            android:layout_weight="0.88">
    
       <!-- Other elements-->
    
       <!-- Parent element-->   
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/etEntrate"
                android:textSize="@dimen/subtitles"
                android:padding="@dimen/activity_horizontal_margin"
                android:layout_alignRight="@+id/data"
                android:layout_alignEnd="@+id/data"
                android:layout_below="@+id/etSpese"
                android:layout_marginRight="@dimen/activity_horizontal_margin"
                android:layout_marginEnd="@dimen/activity_horizontal_margin"
                android:layout_marginTop="@dimen/activity_horizontal_margin"
                  android:layout_marginBottom="@dimen/activity_horizontal_margin"/>
    
      <!--FAB-->
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:clickable="true"
                app:srcCompat="@drawable/plus"
                android:layout_alignTop="@+id/etEntrate"
                android:layout_marginTop="59dp"
                android:layout_alignLeft="@+id/etEntrate"
                android:layout_alignStart="@+id/etEntrate"
                android:layout_alignRight="@+id/etEntrate"
                android:layout_alignEnd="@+id/etEntrate" />
    
       <!--other elements-->
        </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 2014-10-18
      • 2018-10-21
      • 1970-01-01
      • 2020-12-29
      • 2020-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      相关资源
      最近更新 更多