简体   繁体   English

浮动动作按钮Android周围的额外填充

[英]Extra padding around Floating Action Button Android

在此输入图像描述

I am trying to make a profile page in Android. 我正在尝试在Android中创建个人资料页面。 I am using a floating action Button to display user picture. 我正在使用浮动动作按钮来显示用户图片。

Initially i want to display a default picture. 最初我想显示默认图片。

My xml code goes like this: 我的xml代码如下:

<android.support.design.widget.FloatingActionButton
    android:layout_height="100dp"
    android:layout_width="100dp"
    android:id="@+id/userIcon"
    app:fabSize="normal"
    android:src="@drawable/male_icon"
    app:pressedTranslationZ="12dp"
    app:layout_anchor="@id/app_bar"
    app:layout_anchorGravity="bottom|center" />

My question is why there is some extra space around the FAB. 我的问题是为什么FAB周围有一些额外的空间。 Cant i remove that space/padding? 我不能删除那个空间/填充?

EDITED: 编辑:

here is my full xml code:: 这是我的完整xml代码::

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:background="@drawable/background"
        app:contentScrim="#2678AD"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_scrolling" />

<android.support.design.widget.FloatingActionButton
    android:layout_height="100dp"
    android:layout_width="100dp"
    android:id="@+id/userIcon"
    app:fabSize="normal"
    android:src="@drawable/male_icon"
    app:pressedTranslationZ="12dp"
    app:layout_anchor="@id/app_bar"
    app:layout_anchorGravity="bottom|center" />

u might want to change the scaletype attribute to center. 你可能想将scaletype属性更改为center。 Like, 喜欢,

android:scaleType="center"

In layout file set attribute elevation to 0. because it takes default elevation. 在布局文件集属性中提升为0.因为它采用默认高程。

 app:elevation="0dp"

Now in activity check api level greater than 21 and set elevation if needed. 现在在活动中检查api级别大于21并根据需要设置高程。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        fabBtn.setElevation(10.0f);
    }

try this one: 试试这个:

app:borderWidth="0dp"

and you can also remove shadow using elevation 您还可以使用高程删除阴影

app:elevation="6dp"

see this link. 看到这个链接。

if its not working then try to manage outline shadow like this: 如果它不工作然后尝试管理大纲阴影像这样:

Button fab = (Button) rootView.findViewById(R.id.fabbutton);

    Outline mOutlineCircle;
    int shapeSize = getResources().getDimensionPixelSize(R.dimen.shape_size);
    mOutlineCircle = new Outline();
    mOutlineCircle.setRoundRect(0, 0, shapeSize, shapeSize, shapeSize / 2);

    fab.setOutline(mOutlineCircle);
    fab.setClipToOutline(true);

I think this is margin, not padding. 我认为这是保证金,而不是填充。 Try to do it programmatically: 尝试以编程方式执行此操作:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) floatingActionButton.getLayoutParams();
        params.setMargins(0, 0, 0, 0); 
        floatingActionButton.setLayoutParams(params);
    }

Or try to put FloatingActionButton into CoordinatorLayout, like an example below: 或者尝试将FloatingActionButton放入CoordinatorLayout,如下例所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/bt_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="10dp"
        app:backgroundTint="@color/primary"
        app:borderWidth="0dp"
        app:fabSize="mini"/>
</android.support.design.widget.CoordinatorLayout>

Because you are using opposite app:fabSize="normal" . 因为您正在使用相反的app:fabSize="normal" Change to app:fabSize="mini" 更改为app:fabSize="mini"

You can remove the extra padding by allowing your icon to be larger in size. 您可以通过允许图标更大来删除额外的填充。 This way you can set the icon to be the size of the floating action button. 这样,您可以将图标设置为浮动操作按钮的大小。

    app:maxImageSize="40dp"

or whatever size you want to give it. 或者你想给它的任何尺寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM